Class: GOVUKDesignSystemFormBuilder::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/govuk_design_system_formbuilder/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(builder, object_name, attribute_name, &block) ⇒ Base

Returns a new instance of Base.


6
7
8
9
10
11
# File 'lib/govuk_design_system_formbuilder/base.rb', line 6

def initialize(builder, object_name, attribute_name, &block)
  @builder        = builder
  @object_name    = object_name
  @attribute_name = attribute_name
  @block_content  = capture { block.call } if block_given?
end

Instance Method Details

#field_id(link_errors: false) ⇒ String

Note:

field_id is overridden so that the error summary can link to the correct element.

It's straightforward for inputs with a single element (like a textarea or text input) but the GOV.UK Design System requires that the error summary link to the first checkbox or radio in a list, so additional logic is requred

returns the id value used for the input

Returns:

  • (String)

    the element's id

See Also:


31
32
33
34
35
36
37
# File 'lib/govuk_design_system_formbuilder/base.rb', line 31

def field_id(link_errors: false)
  if link_errors && has_errors?
    build_id('field-error', include_value: false)
  else
    build_id('field')
  end
end

#to_sObject

objects that implement #to_s can be passed directly into #safe_join


14
15
16
# File 'lib/govuk_design_system_formbuilder/base.rb', line 14

def to_s
  html || ''
end