Add another (experimental)
The "add another" component lets users input multiple values for a set of form fields.
This component is currently experimental because more research is needed to validate it.
Applications using this component must include a deletion checkbox in addtion to the rendered repeating items as well as an empty field. The checkboxes and empty field are required to allow users without javascript to add new items and remove existing items from the list. See the examples below for how to do this.
The example here passes HTML in to the component due to limitations in the format of this documentation. In applications it is expected that the caller will render other components instead. See Whitehall for examples of this approach.
How it looks (preview) (preview all)
How to call this component
<%= render "govuk_publishing_components/components/add_another", {
fieldset_legend: "Person",
add_button_text: "Add another person",
items: [
{
fields: sanitize("<div class=\"govuk-form-group\">
<label for=\"person_0_name\" class=\"gem-c-label govuk-label\">Full name</label>
<input class=\"gem-c-input govuk-input\" id=\"person_0_name\" name=\"person[0]name\">
</div>
"),
destroy_checkbox: sanitize("<div class=\"govuk-checkboxes\" data-module=\"govuk-checkboxes\">
<div class=\"govuk-checkboxes__item\">
<input type=\"checkbox\" name=\"person[0][_destroy]\" id=\"person_0__destroy\" class=\"govuk-checkboxes__input\">
<label for=\"person_0__destroy\" class=\"govuk-label govuk-checkboxes__label\">Delete</label>
</div>
</div>
")
}
],
empty: sanitize("<div class=\"govuk-form-group\"> <label for=\"person_1_name\" class=\"gem-c-label govuk-label\">Full name</label> <input class=\"gem-c-input govuk-input\" id=\"person_1_name\" name=\"person[1]name\"> </div>")
} %>
GOV.UK Design System
This component incorporates components from the GOV.UK Design System:
Accessibility acceptance criteria
The form controls within the fieldsets must be fully accessible as per the design system guidance for each of the form control components.
Other examples
Standard options
This component uses the component wrapper helper. It accepts the following options and applies them to the parent element of the component. See the component wrapper helper documentation for more detail.
id
- accepts a string for the element ID attributedata_attributes
- accepts a hash of data attributesaria
- accepts a hash of aria attributesclasses
- accepts a space separated string of classes, these should not be used for styling and must be prefixed withjs-
margin_bottom
- accepts a number from0
to9
(0px
to60px
) using the GOV.UK Frontend spacing scalerole
- accepts a space separated string of roleslang
- accepts a language attribute valueopen
- accepts an open attribute value (true or false)hidden
- accepts an empty string, ‘hidden’, or ‘until-found’tabindex
- accepts an integer. The integer can also be passed as a stringdir
- accepts ‘rtl’, ‘ltr’, or ‘auto’type
- accepts any valid type attribute e.g. ‘button’, ‘submit’, ‘text’rel
- accepts any valid rel attribute e.g. ‘nofollow’target
- accepts a valid target attribute e.g. ‘_blank’title
- accepts any stringdraggable
- accepts a draggable attribute value (“true” or “false”)
Start empty (preview)
By default no form fields are displayed when the component loads if no content is specified
<%= render "govuk_publishing_components/components/add_another", {
fieldset_legend: "Employee",
add_button_text: "Add an employee",
empty_fields: true,
items: null,
empty: sanitize("<div class=\"govuk-form-group\"> <label for=\"employee_1_name\" class=\"gem-c-label govuk-label\">Full name</label> <input class=\"gem-c-input govuk-input\" id=\"employee_1_name\" name=\"employee[1]name\"> </div>")
} %>