Form date input
Use the date input component to help users enter a memorable date or one they can easily look up.
Overview
How it looks (preview) (preview all)
How to call this component
<%= render "govuk_publishing_components/components/date_input", {} %>
GOV.UK Design System
This component incorporates components from the GOV.UK Design System:
Accessibility acceptance criteria
Inputs in the component must:
- accept focus
- be focusable with a keyboard
- be usable with a keyboard
- be usable with touch
- indicate when they have focus
- be recognisable as form input elements
- have correctly associated labels
- be of the appropriate type for their use, in this case ‘number’
Labels use the label component.
Avoid using autofocus and tabindex unless you have user research to support this behaviour.
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.
See all standard options
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 from0to9(0pxto60px) 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”)
With name (preview)
Settting a name at the component level helps generating the name for each individual input within the
component as follows: custom-name[day], custom-name[month], custom-name[year]
<%= render "govuk_publishing_components/components/date_input", {
name: "dob"
} %>
With autocomplete for date of birth fields (preview)
Use the autocomplete_date_of_birth option when you’re asking for the user’s date of birth. This supports browsers or tools that can autofill the information on a user’s behalf if they’ve entered it previously.
Note that the option should only be passed to the component if the date of birth asked for is the date of birth of the person filling the form in and not asked for on behalf of someone else.
<%= render "govuk_publishing_components/components/date_input", {
autocomplete_date_of_birth: true
} %>
With legend (preview)
<%= render "govuk_publishing_components/components/date_input", {
legend_text: "What is your date of birth?"
} %>
With legend level and size (preview)
Valid options for level are 1 to 6. Valid options for size are s, m, l and xl.
<%= render "govuk_publishing_components/components/date_input", {
legend_text: "What is your date of birth?",
legend_heading_level: 2,
legend_size: "l"
} %>
With hint (preview)
When hint text is rendered each input field takes the value of hint_id as a value for the aria-describedby attribute
<%= render "govuk_publishing_components/components/date_input", {
legend_text: "What is your date of birth?",
hint: "For example, 31 3 1980"
} %>
With error (preview)
When an error message is rendered each input field takes the value of error_id as a value for the aria-describedby attribute. Note that error_id is optional, if it is not passed one will be generated automatically.
<%= render "govuk_publishing_components/components/date_input", {
legend_text: "What is your date of birth?",
hint: "For example, 31 3 1980",
error_id: "error_item_id",
error_message: "Error message goes here"
} %>
With error items (preview)
When error messages are rendered each input field takes the value of error_id as a value for the aria-describedby attribute. Note that error_id is optional, if it is not passed one will be generated automatically.
<%= render "govuk_publishing_components/components/date_input", {
legend_text: "What is your date of birth?",
hint: "For example, 31 3 1980",
error_id: "error_id",
error_items: [
{
text: "Error 1"
},
{
text: "Error 2"
}
]
} %>
With error id but no message (preview)
For some instances of this component the error message should be rendered separately but an error state is still required. In this scenario an error_id can be passed without an error_message to highlight the component and set aria-describedby correctly.
<%= render "govuk_publishing_components/components/date_input", {
legend_text: "What is your date of birth?",
hint: "For example, 31 3 1980",
error_id: "error_id"
} %>
With data attributes (preview)
<%= render "govuk_publishing_components/components/date_input", {
data_attributes: {
module: "some-module",
other_attribute: "something-else"
}
} %>
With custom items (preview)
<%= render "govuk_publishing_components/components/date_input", {
legend_text: "Beth yw eich dyddiad geni?",
hint: "Er enghraifft, 31 3 1980",
items: [
{
label: "Dydd",
name: "dob-dydd",
width: 2,
value: 31
},
{
label: "Mis",
name: "dob-mis",
width: 2,
value: 3
},
{
label: "Blwyddyn",
name: "dob-blwyddyn",
width: 4,
value: 1980
}
]
} %>