Class: Forms::FormControl

Inherits:
Phlex::HTML
  • Object
show all
Defined in:
lib/forms/form_control.rb

Overview

Wraps a field in the daisyui form-control layout: optional label on top, the field (yielded block), then an error (if present) or a hint. This is the workhorse behind the Control-first f.field API and the explicit f.Control escape hatch.

Direct Known Subclasses

Plain::Control

Instance Method Summary collapse

Constructor Details

#initialize(*modifiers, label: nil, hint: nil, error: nil, for: nil, required: false, label_id: nil, hint_id: nil, **options) ⇒ FormControl

label_id:/hint_id: give the label and hint stable ids so a group control (checkbox_group's div, which a plain for/id can't name) can reference them via aria-labelledby / aria-describedby (issue #17).



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/forms/form_control.rb', line 12

def initialize(*modifiers, label: nil, hint: nil, error: nil, for: nil, required: false,
               label_id: nil, hint_id: nil, **options)
  @modifiers = modifiers
  @label = label
  @hint = hint
  @error = error
  @field_id = grab(for:)
  @required = required
  @label_id = label_id
  @hint_id = hint_id
  @options = options
  super()
end

Instance Method Details

#view_templateObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/forms/form_control.rb', line 26

def view_template(&)
  div(class: control_classes, **@options.except(:class)) do
    render Forms::Label.new(text: @label, for: @field_id, required: @required, id: @label_id) if @label

    yield if block_given?

    if @error
      render Forms::FieldError.new(message: @error)
    elsif @hint
      render Forms::FieldHint.new(text: @hint, id: @hint_id)
    end
  end
end