Class: UI::FieldError

Inherits:
Phlex::HTML
  • Object
show all
Includes:
FieldErrorBehavior
Defined in:
app/components/ui/field_error.rb

Overview

Error - Phlex implementation

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

Examples:

With single error

render UI::Error.new(errors: ["Email is required"])

With multiple errors

render UI::Error.new(errors: ["Email is required", "Email is invalid"])

With block

render UI::Error.new { "Error message" }

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) ⇒ FieldError

Returns a new instance of FieldError.

Parameters:

  • errors (Array) (defaults to: nil)

    Array of error messages

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

    Additional CSS classes to merge

  • attributes (Hash)

    Additional HTML attributes



22
23
24
25
26
27
# File 'app/components/ui/field_error.rb', line 22

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

Instance Method Details

#view_template(&block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'app/components/ui/field_error.rb', line 29

def view_template(&block)
  @block_given = block_given?
  return unless has_errors? || @block_given

  if has_errors?
    render_errors
  else
    render_content(&block)
  end
end