Class: Brut::FrontEnd::Components::ConstraintViolations
- Inherits:
-
Brut::FrontEnd::Component
- Object
- Phlex::HTML
- Brut::FrontEnd::Component
- Brut::FrontEnd::Components::ConstraintViolations
- 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
-
#initialize(form:, input_name:, index: nil, message_html_attributes: {}, **html_attributes) ⇒ ConstraintViolations
constructor
Create a new ConstraintViolations component.
-
#view_template ⇒ Object
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
Methods included from I18n::BaseMethods
#l, #t, #t_direct, #this_field_value
Methods included from Brut::Framework::Errors
Constructor Details
#initialize(form:, input_name:, index: nil, message_html_attributes: {}, **html_attributes) ⇒ ConstraintViolations
Create a new ConstraintViolations component
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 = .map {|name,value| [ name.to_sym, value ] }.to_h end |
Instance Method Details
#view_template ⇒ Object
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) = { "server-side": true, }.merge(@message_html_attributes) (**html_attributes) do @form.input(@input_name, index: @index).validity_state.select { |constraint| !constraint.client_side? }.each do |constraint| brut_cv(**) do t("cv.be.#{constraint}", **constraint.context) end end end end |