Module: UI::FieldErrorBehavior
- Included in:
- FieldError, FieldErrorComponent
- Defined in:
- app/behaviors/ui/field_error_behavior.rb
Overview
UI::FieldErrorBehavior
Instance Method Summary collapse
-
#error_message(error) ⇒ Object
Returns error message from error object (supports Hash or String).
-
#field_error_classes ⇒ Object
Returns combined CSS classes for the field error.
-
#field_error_html_attributes ⇒ Object
Returns HTML attributes for the field error element.
-
#has_errors? ⇒ Boolean
Returns true if errors should be displayed.
-
#multiple_errors? ⇒ Boolean
Returns true if displaying multiple errors.
-
#single_error? ⇒ Boolean
Returns true if displaying a single error.
Instance Method Details
#error_message(error) ⇒ Object
Returns error message from error object (supports Hash or String)
49 50 51 |
# File 'app/behaviors/ui/field_error_behavior.rb', line 49 def (error) error.is_a?(Hash) ? error[:message] : error end |
#field_error_classes ⇒ Object
Returns combined CSS classes for the field error
26 27 28 29 30 31 |
# File 'app/behaviors/ui/field_error_behavior.rb', line 26 def field_error_classes classes_value = respond_to?(:classes, true) ? classes : @classes base = "text-destructive text-sm font-normal" TailwindMerge::Merger.new.merge([base, classes_value].compact.join(" ")) end |
#field_error_html_attributes ⇒ Object
Returns HTML attributes for the field error element
16 17 18 19 20 21 22 23 |
# File 'app/behaviors/ui/field_error_behavior.rb', line 16 def field_error_html_attributes attributes_value = respond_to?(:attributes, true) ? attributes : @attributes { "data-slot": "field-error", class: field_error_classes, role: "alert" }.merge(attributes_value).compact end |
#has_errors? ⇒ Boolean
Returns true if errors should be displayed
34 35 36 |
# File 'app/behaviors/ui/field_error_behavior.rb', line 34 def has_errors? @errors.is_a?(Array) && @errors.any? end |
#multiple_errors? ⇒ Boolean
Returns true if displaying multiple errors
44 45 46 |
# File 'app/behaviors/ui/field_error_behavior.rb', line 44 def multiple_errors? has_errors? && @errors.length > 1 end |
#single_error? ⇒ Boolean
Returns true if displaying a single error
39 40 41 |
# File 'app/behaviors/ui/field_error_behavior.rb', line 39 def single_error? has_errors? && @errors.length == 1 end |