Class: CCS::Components::GovUK::FormGroup

Inherits:
Base
  • Object
show all
Defined in:
lib/ccs/components/govuk/form_group.rb

Overview

GOV.UK FormGroup

This helper is used for generating the form group component from the Government Design Systems

Constant Summary collapse

DEFAULT_ATTRIBUTES =

The default attributes for the form group

{ class: 'govuk-form-group' }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(attribute:, error_message: nil, **options) ⇒ FormGroup

Returns a new instance of FormGroup.

Parameters:

  • attribute (String, Symbol)

    the attribute that the form group is for

  • error_message (String) (defaults to: nil)

    (nil) the error message to be displayed

  • options (Hash)

    options that will be used in customising the HTML

Options Hash (**options):

  • :classes (String)

    additional CSS classes for the form group HTML

  • :attributes (Hash) — default: {}

    any additional attributes that will added as part of the HTML



30
31
32
33
34
35
36
37
38
# File 'lib/ccs/components/govuk/form_group.rb', line 30

def initialize(attribute:, error_message: nil, **options)
  super(**options)

  @options[:attributes][:id] ||= "#{attribute}-form-group"
  @options[:attributes][:class][..15] = 'govuk-form-group govuk-form-group--error' if error_message

  @attribute = attribute
  @error_message = error_message
end

Instance Method Details

#render {|displayed_error_message| ... } ⇒ ActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Form Group component

Yields:

  • HTML that will be contained within the ‘govuk-form-group’ div

Yield Parameters:

  • displayed_error_message (ActiveSupport::SafeBuffer)

    the HTML for the error message (if there is one)

Returns:

  • (ActiveSupport::SafeBuffer)


48
49
50
51
52
# File 'lib/ccs/components/govuk/form_group.rb', line 48

def render
  tag.div(**options[:attributes]) do
    yield((ErrorMessage.new(message: error_message, attribute: attribute, context: context).render if error_message))
  end
end