Module: UI::FieldErrorBehavior

Included in:
FieldError, FieldErrorComponent
Defined in:
app/behaviors/ui/field_error_behavior.rb

Overview

UI::FieldErrorBehavior

Instance Method Summary collapse

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_message(error)
  error.is_a?(Hash) ? error[:message] : error
end

#field_error_classesObject

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_attributesObject

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


39
40
41
# File 'app/behaviors/ui/field_error_behavior.rb', line 39

def single_error?
  has_errors? && @errors.length == 1
end