Class: PhlexKit::FieldError

Inherits:
BaseComponent show all
Defined in:
app/components/phlex_kit/field/field_error.rb

Overview

Validation message(s) for a Field, ported from shadcn/ui's FieldError. Pass errors: an array of message strings (deduped; one renders plain, several render a list) or give a block. Renders nothing when empty — matching theirs. Distinct from FormFieldError, which is the Stimulus live-validation slot; server-rendered errors belong here. See field.rb.

Instance Method Summary collapse

Methods inherited from BaseComponent

#on

Constructor Details

#initialize(errors: nil, **attrs) ⇒ FieldError

Returns a new instance of FieldError.



8
9
10
11
# File 'app/components/phlex_kit/field/field_error.rb', line 8

def initialize(errors: nil, **attrs)
  @errors = Array(errors).compact.uniq
  @attrs = attrs
end

Instance Method Details

#view_template(&block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/components/phlex_kit/field/field_error.rb', line 13

def view_template(&block)
  return if @errors.empty? && !block

  div(**mix({ class: "pk-field-error", role: "alert", data: { slot: "field-error" } }, @attrs)) do
    if block
      yield
    elsif @errors.length == 1
      plain @errors.first
    else
      ul(class: "pk-field-error-list") do
        @errors.each { |message| li { message } }
      end
    end
  end
end