Class: PhlexKit::FormFieldError

Inherits:
BaseComponent show all
Defined in:
app/components/phlex_kit/form_field/form_field_error.rb

Overview

Validation error text for a PhlexKit::FormField, styled with the destructive token and hidden when empty. Render it empty for live client-side messages (the phlex-kit--form-field controller fills it once the control goes invalid), or with server-rendered model errors — non-empty content arms validation on connect. See form_field.rb.

Instance Method Summary collapse

Methods inherited from BaseComponent

#on

Constructor Details

#initialize(id: nil, **attrs) ⇒ FormFieldError

Returns a new instance of FormFieldError.



8
9
10
11
12
13
# File 'app/components/phlex_kit/form_field/form_field_error.rb', line 8

def initialize(id: nil, **attrs)
  # A default id (like SelectContent / CommandList) so the controller can
  # always wire aria-describedby from the control to this message.
  @id = id || "pk-form-field-error-#{SecureRandom.hex(4)}"
  @attrs = attrs
end

Instance Method Details

#view_template(&block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'app/components/phlex_kit/form_field/form_field_error.rb', line 15

def view_template(&block)
  # role="alert": the controller swaps this text live on invalid/input —
  # without a live region the announcement never reaches screen readers.
  p(**mix({
    id: @id,
    class: "pk-form-field-error",
    role: "alert",
    data: { phlex_kit__form_field_target: "error" }
  }, @attrs), &block)
end