All form helpers that generate a single input element
automatically associate it with a corresponding label.
By default, the label text is set to the capitalised name of the
attribute. If the attribute contains underscores they will be left
in. This is intentional behaviour to prompt the developer to add
some more suitable text.
Labels are customised by passing in a hash of options with the
following keys:
text
The actual text that forms the label
tag
The HTML tag that the label will be wrapped in. This is useful
on pages where the label is also the main header where the tag
should be h1
size
The label size which follows the usual GOV.UK pattern of
xl, l, m and s
hidden
Labels can be visually hidden when their presence would complicate
the form. This is a common pattern when asking for multiple lines
of address where a single label or legend will suffice. The hidden
value defaults to false and can be overridden with
true
Label content can be customised further using a
Ruby proc. Note that the content
of the proc is rendered inside the label so the form builder
can enforce consistency between the label’s for attribute
and the associated form element.
There are occasions where placing the label above an input won’t work. We can
do this by suppressing the default label with label: nil and manually adding
the label elsewhere by calling the govuk_label helper directly.
Captions
Sometimes you may need to make it clear that a heading is part of a
larger section or group. To do this, you can use a heading with a caption.
Captions are supported by all helpers that have labels or legends and can
be configured with the following options:
Hints are used to provide extra information that’s relevant to the
majority of users, like how their information will be used, or where to
find it.
Hints contents be customised further by passing in a proc
instead of a string.
To accomodate screenreaders and users of other assistive technology,
hints must be associated with inputs via the
aria-describedby attribute. The form builder does this
automatically.
Legends look, act and work in a similar fashion to labels but they
describe a group of inputs.
The GOV.UK Design System uses legends to describe radio button, check box
and date fields but any group of related fields can be wrapped in a
fieldset.
Legends are customised by passing in a hash of options with the
following keys:
text
The actual text that forms the legend
tag
The HTML tag that the legend will be wrapped in. This is useful
on pages where the label is also the main header. In cases like
this, it should be set to h1
size
The legend size which follows the usual GOV.UK pattern of
xl, l, m and s
hidden
Legends can be visually hidden when their presence would complicate
the form. The hidden value defaults to false and can be
overridden with true
To take even more control of legends the content can be passed in using
a Ruby proc. In this example
we’re using Slim but could just as easily call a helper method or
render a partial, component or string of HTML.
Procs allow for complete control of the legend’s HTML, including the
legend element itself. This means that the legend element needs to be
added manually and it should wrap all of the fieldset’s heading text.