Module: GOVUKDesignSystemFormBuilder::BuilderHelper

Defined in:
lib/govuk_design_system_formbuilder/builder_helper.rb

Overview

NOTE: this is currently considered experimental, it’s likely to change based on feedback.

BuilderHelper contains methods that expose the form builder’s functionality externally. The objectives are to allow:

  • rendering the error summary outside of the form

  • setting the id of custom form elements (rich text editors, date pickers, sliders, etc) using the formbuilder’s internal logic, allowing them to be linked from the error summary

Instance Method Summary collapse

Instance Method Details

#govuk_error_summary(object, object_name = nil, *args, **kwargs, &block) ⇒ Object

Renders an error summary

Examples:

= govuk_error_summary(@registration)

Parameters:

  • object (ActiveRecord::Base, ActiveModel::Model, Object)

    the object we’ll be rendering the errors for

  • object_name (Symbol) (defaults to: nil)

    the object’s name, the singular version of the object’s class name, e.g., Person is :person. If none is supplied we’ll try to infer it from the object, so it’ll probably be necessary for regular Ruby objects

  • args (Hash)

    a customizable set of options

  • kwargs (Hash)

    a customizable set of options

Options Hash (*args):

  • options (Array)

    passed through to the builder’s #govuk_error_summary

Options Hash (**kwargs):

  • keyword (Hash)

    options passed through to the builder’s #govuk_error_summary

See Also:



45
46
47
48
49
# File 'lib/govuk_design_system_formbuilder/builder_helper.rb', line 45

def govuk_error_summary(object, object_name = nil, *args, **kwargs, &block)
  (object_name = retrieve_object_name(object)) if object_name.nil?

  proxy_builder(object, object_name, self, {}).govuk_error_summary(*args, **kwargs, &block)
end

#govuk_field_id(object, attribute_name, object_name = nil, value: nil, link_errors: true) ⇒ Object

Returns the form builder generated id for an object’s attribute, allowing users to force their custom element ids to match those that’ll be generated by the error summary.

Parameters:

  • object (ActiveRecord::Base, ActiveModel::Model, Object)

    the object that we want to generate an id for

  • object_name (Symbol) (defaults to: nil)

    the object’s name, the singular version of the object’s class name, e.g., Person is :person.

  • attribute_name (Symbol)

    the attribute we’re generating an id for

  • value (Object) (defaults to: nil)

    the value of the attribute. Only necessary for fields with multiple form elements like radio buttons and checkboxes

  • link_errors (Boolean) (defaults to: true)

    toggles whether or not to override the field id with the error id when there are errors on the object. Only relevant for radio buttons and check boxes.



26
27
28
29
30
# File 'lib/govuk_design_system_formbuilder/builder_helper.rb', line 26

def govuk_field_id(object, attribute_name, object_name = nil, value: nil, link_errors: true)
  (object_name = retrieve_object_name(object)) if object_name.nil?

  proxy_base(object, object_name, attribute_name, value:).field_id(link_errors:)
end