Module: CCS::FrontendHelpers::GovUKFrontend::FormGroup

Included in:
CCS::FrontendHelpers::GovUKFrontend
Defined in:
lib/ccs/frontend_helpers/govuk_frontend/form_group.rb

Overview

GOV.UK FormGroup

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

Instance Method Summary collapse

Instance Method Details

#govuk_form_group(attribute, **options) {|displayed_error_message| ... } ⇒ ActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Warning text component

Parameters:

  • attribute (String, Symbol)

    the attribute that the form group is for

  • error_message (String)

    (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

  • :model (ActiveModel) — default: nil

    model that will be used to find an error message

  • :attributes (Hash) — default: {}

    any additional attributes that will added as part of the HTML

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)


27
28
29
30
31
32
33
34
35
# File 'lib/ccs/frontend_helpers/govuk_frontend/form_group.rb', line 27

def govuk_form_group(attribute, **options, &)
  error_message = if options[:model]
                    options[:model].errors[attribute].first
                  else
                    options[:error_message]
                  end

  Components::GovUK::FormGroup.new(context: self, attribute: attribute, error_message: error_message, **options).render(&)
end