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-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:
<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
-
#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
#around_template, component_name, #component_name
Methods included from Brut::FrontEnd::Component::Helpers
#entity, #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
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 = .map {|name,value| [ name.to_sym, value ] }.to_h end |
Instance Method Details
#view_template ⇒ Object
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) = { "server-generated": true, }.merge(@message_html_attributes) (**html_attributes) do @form.input(@input_name, index: @index).validity_state.each do |constraint| if constraint.client_side? if !@form.new? brut_cv(**, client_side: true) do t("cv.cs.#{constraint}", **constraint.context) end end else brut_cv(**, server_side: true) do t("cv.ss.#{constraint}", **constraint.context) end end end end end |