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-generated>` tag that will contain the I18n translation of the violation’s Forms::ConstraintViolation#key prefixed with ‘“cv.cs”` or `“cv.ss”`.

The general form of this component will be:

“‘html <brut-cv-messages input-name=“«input_name»”>

<brut-cv server-generated client-side>
  «message»
</brut-cv>
<brut-cv server-generated server-side>
  «message»
</brut-cv>
<!- ... ->

</brut-cv-messages> “‘

Notes:

  • If the form is considered #Form#new?, then the client-side constraint violations will not be generated. This is to prevent a fresh form from being generated with a bunch of errors already present.

  • If using ‘<brut-form>`, the `<brut-cv-messages>` element this generates will be where it inserts client side constraint violations.

Instance Method Summary collapse

Methods inherited from Brut::FrontEnd::Component

#around_template, component_name, #component_name

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

#entity, #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.



32
33
34
35
36
37
38
39
# File 'lib/brut/front_end/components/constraint_violations.rb', line 32

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/brut/front_end/components/constraint_violations.rb', line 41

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

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

  brut_cv_messages(**html_attributes) do
    @form.input(@input_name, index: @index).validity_state.each do |constraint|
      if constraint.client_side?
        if !@form.new?
          brut_cv(**message_html_attributes, client_side: true) do
            t("cv.cs.#{constraint}", **constraint.context)
          end
        end
      else
        brut_cv(**message_html_attributes, server_side: true) do
          t("cv.ss.#{constraint}", **constraint.context)
        end
      end
    end
  end
end