Class: UI::FieldErrorComponent

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
FieldErrorBehavior
Defined in:
app/view_components/ui/field_error_component.rb

Overview

FieldErrorComponent - ViewComponent implementation

Error message display for form fields with accessibility support. Uses FieldErrorBehavior concern for shared styling logic.

Examples:

With single error

<%= render UI::FieldErrorComponent.new(errors: ["Email is required"]) %>

With multiple errors

<%= render UI::FieldErrorComponent.new(errors: ["Email is required", "Email is invalid"]) %>

With block

<%= render UI::FieldErrorComponent.new do %>
  Error message
<% end %>

Instance Method Summary collapse

Methods included from FieldErrorBehavior

#error_message, #field_error_classes, #field_error_html_attributes, #has_errors?, #multiple_errors?, #single_error?

Constructor Details

#initialize(errors: nil, classes: "", **attributes) ⇒ FieldErrorComponent

Returns a new instance of FieldErrorComponent.

Parameters:

  • errors (Array) (defaults to: nil)

    Array of error messages

  • classes (String) (defaults to: "")

    Additional CSS classes to merge

  • attributes (Hash)

    Additional HTML attributes



24
25
26
27
28
# File 'app/view_components/ui/field_error_component.rb', line 24

def initialize(errors: nil, classes: "", **attributes)
  @errors = errors
  @classes = classes
  @attributes = attributes
end

Instance Method Details

#callObject



30
31
32
33
34
35
36
37
38
# File 'app/view_components/ui/field_error_component.rb', line 30

def call
  return unless has_errors? || content?

  if has_errors?
    render_errors
  else
    render_content
  end
end