Class: CCS::Components::GovUK::Field

Inherits:
Base
  • Object
show all
Defined in:
lib/ccs/components/govuk/field.rb,
lib/ccs/components/govuk/field/input.rb,
lib/ccs/components/govuk/field/inputs.rb,
lib/ccs/components/govuk/field/inputs/item.rb,
lib/ccs/components/govuk/field/input/select.rb,
lib/ccs/components/govuk/field/inputs/radios.rb,
lib/ccs/components/govuk/field/input/textarea.rb,
lib/ccs/components/govuk/field/input/text_input.rb,
lib/ccs/components/govuk/field/input/file_upload.rb,
lib/ccs/components/govuk/field/inputs/checkboxes.rb,
lib/ccs/components/govuk/field/inputs/date_input.rb,
lib/ccs/components/govuk/field/inputs/item/radio.rb,
lib/ccs/components/govuk/field/inputs/item/divider.rb,
lib/ccs/components/govuk/field/input/password_input.rb,
lib/ccs/components/govuk/field/input/text_input/fix.rb,
lib/ccs/components/govuk/field/inputs/item/checkbox.rb,
lib/ccs/components/govuk/field/input/character_count.rb,
lib/ccs/components/govuk/field/inputs/item/radio/tag.rb,
lib/ccs/components/govuk/field/inputs/date_input/item.rb,
lib/ccs/components/govuk/field/inputs/item/radio/form.rb,
lib/ccs/components/govuk/field/inputs/item/checkbox/tag.rb,
lib/ccs/components/govuk/field/inputs/item/checkbox/form.rb,
lib/ccs/components/govuk/field/input/character_count/count_message.rb,
lib/ccs/components/govuk/field/input/password_input/show_hide_button.rb

Overview

GOV.UK Field

This class is used to create a form using the structure of a GDS input field components, e.g. text input. It will wrap the input within the form group as well as find if there are any error messages to display.

Direct Known Subclasses

Input, Inputs

Defined Under Namespace

Classes: Input, Inputs

Constant Summary

Constants inherited from Base

Base::DEFAULT_ATTRIBUTES

Instance Method Summary collapse

Constructor Details

#initialize(attribute:, form_group: nil, hint: nil, before_input: nil, after_input: nil, **options) ⇒ Field

Returns a new instance of Field.

Parameters:

  • attribute (String, Symbol)

    the attribute of the field

  • hint (Hash) (defaults to: nil)

    attributes for the hint, see Hint#initialize for more details. If no hint is given then no hint will be rendered

  • form_group (Hash) (defaults to: nil)

    attributes for the form group, see FormGroup#initialize for more details.

  • options (Hash)

    options that will be used for the parts of the field

Options Hash (**options):

  • :error_message (String) — default: nil

    the error message to be displayed

  • :model (ActiveModel) — default: nil

    optional model that can be used to find an error message

  • :form (ActionView::Helpers::FormBuilder) — default: nil

    optional form builder used to create the field and find the error message

  • :classes (String)

    additional CSS classes for the field HTML

  • :attributes (Hash) — default: {}

    any additional attributes that will added as part of the HTML



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ccs/components/govuk/field.rb', line 48

def initialize(attribute:, form_group: nil, hint: nil, before_input: nil, after_input: nil, **options)
  super(**options)

  (hint[:attributes] ||= {})[:id] = "#{attribute}-hint" if hint && !hint.dig(:attributes, :id)

  @attribute = attribute
  @error_message = find_error_message(attribute)

  @form_group = FormGroup.new(attribute: attribute, error_message: @error_message, context: @context, **(form_group || {}))
  @hint = Hint.new(context: @context, **hint) if hint
  @before_input = before_input
  @after_input = after_input
end

Instance Method Details

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

Generates the HTML to wrap arround a GDS form input component

Yields:

  • the field HTML

Yield Parameters:

  • displayed_error_message (ActiveSupport::SafeBuffer)

    the error message HTML to display

Returns:

  • (ActiveSupport::SafeBuffer)


72
73
74
# File 'lib/ccs/components/govuk/field.rb', line 72

def render(&)
  form_group.render(&)
end