Class: Brut::FrontEnd::Components::ConstraintViolations

Inherits:
Brut::FrontEnd::Component show all
Defined in:
lib/brut/front_end/components/constraint_violations.rb

Overview

Renders the custom elements used to manage both client- and server-side constraint violations via the <brut-cv-messages> and <brut-cv> tags. Each constraint violation on the input's Forms::ValidityState will generate a <brut-cv server-side> tag that will contain the I18n translation of the violation's Forms::ConstraintViolation#key prefixed with "cv.be".

The general form of this component will be:

<brut-cv-messages input-name="«input_name»">
  <brut-cv server-side>
    «message»
  </brut-cv>
  <brut-cv server-side>
    «message»
  </brut-cv>
  <!- ... ->
</brut-cv-messages>

Note that if you are using <brut-form> then <brut-cv> elements will be inserted into the <brut-cv-messages> element, however they will not have the server-side attribute.

Instance Method Summary collapse

Methods inherited from Brut::FrontEnd::Component

component_name, #component_name

Methods included from Brut::FrontEnd::Component::Helpers

#global_component, #inline_svg

Methods included from I18n::ForHTML

#html_escape, #t

Methods included from I18n::BaseMethods

#l, #t, #t_direct, #this_field_value

Methods included from Brut::Framework::Errors

#abstract_method!, #bug!

Constructor Details

#initialize(form:, input_name:, index: nil, message_html_attributes: {}, **html_attributes) ⇒ ConstraintViolations

Create a new ConstraintViolations component

Parameters:

  • form (Brut::FrontEnd::Form)

    the form in which this component is being rendered.

  • input_name (String|Symbol)

    the name of the input, based on what was used in the form object.

  • html_attributes (Hash)

    attributes to be placed on the outer <brut-cv-messages> element.

  • index (Integer) (defaults to: nil)

    index of the input, for array-based inputs

  • message_html_attributes (Hash) (defaults to: {})

    attributes to be placed on each inner <brut-cv> element.



26
27
28
29
30
31
32
33
# File 'lib/brut/front_end/components/constraint_violations.rb', line 26

def initialize(form:, input_name:, index: nil, message_html_attributes: {}, **html_attributes)
  @form                    =  form
  @input_name              =  input_name
  @array                   = !index.nil?
  @index                   =  index || 0
  @html_attributes         =  html_attributes.map {|name,value| [ name.to_sym, value ] }.to_h
  @message_html_attributes =  message_html_attributes.map {|name,value| [ name.to_sym, value ] }.to_h
end

Instance Method Details

#view_templateObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/brut/front_end/components/constraint_violations.rb', line 35

def view_template
  html_attributes = {
    "input-name": @array ? "#{@input_name}[]" : @input_name.to_s,
  }.merge(@html_attributes)

  message_html_attributes = {
    "server-side": true,
  }.merge(@message_html_attributes)

  brut_cv_messages(**html_attributes) do
    @form.input(@input_name, index: @index).validity_state.select { |constraint|
      !constraint.client_side?
    }.each do |constraint|
      brut_cv(**message_html_attributes) do
        t("cv.be.#{constraint}", **constraint.context)
      end
    end
  end
end