# Date input ## Introduction A component for entering dates, for example - date of birth. ## Guidance Find out when to use the date input component in your service in the [GOV.UK Design System](https://design-system.service.gov.uk/components/date-input). ## Quick start examples ### Date input [Preview this example in the Frontend review app](http://govuk-frontend-review.herokuapp.com/components/date-input/preview) #### Markup
What is your date of birth? For example, 31 3 1980
#### Macro {% from "date-input/macro.njk" import govukDateInput %} {{ govukDateInput({ "id": "dob", "namePrefix": "dob", "fieldset": { "legend": { "text": "What is your date of birth?" } }, "hint": { "text": "For example, 31 3 1980" }, "items": [ { "name": "day", "classes": "govuk-input--width-2" }, { "name": "month", "classes": "govuk-input--width-2" }, { "name": "year", "classes": "govuk-input--width-4" } ] }) }} ### Date input with errors [Preview this example in the Frontend review app](http://govuk-frontend-review.herokuapp.com/components/date-input/with-errors/preview) #### Markup
What is your date of birth? For example, 31 3 1980 Error message goes here
#### Macro {% from "date-input/macro.njk" import govukDateInput %} {{ govukDateInput({ "id": "dob-errors", "fieldset": { "legend": { "text": "What is your date of birth?" } }, "hint": { "text": "For example, 31 3 1980" }, "errorMessage": { "text": "Error message goes here" }, "items": [ { "name": "day", "classes": "govuk-input--width-2 govuk-input--error" }, { "name": "month", "classes": "govuk-input--width-2 govuk-input--error" }, { "name": "year", "classes": "govuk-input--width-4 govuk-input--error" } ] }) }} ### Date input with error on day input [Preview this example in the Frontend review app](http://govuk-frontend-review.herokuapp.com/components/date-input/with-error-on-day-input/preview) #### Markup
What is your date of birth? For example, 31 3 1980 Error message goes here
#### Macro {% from "date-input/macro.njk" import govukDateInput %} {{ govukDateInput({ "id": "dob-day-error", "namePrefix": "dob-day-error", "fieldset": { "legend": { "text": "What is your date of birth?" } }, "hint": { "text": "For example, 31 3 1980" }, "errorMessage": { "text": "Error message goes here" }, "items": [ { "name": "day", "classes": "govuk-input--width-2 govuk-input--error" }, { "name": "month", "classes": "govuk-input--width-2" }, { "name": "year", "classes": "govuk-input--width-4" } ] }) }} ### Date input with error on month input [Preview this example in the Frontend review app](http://govuk-frontend-review.herokuapp.com/components/date-input/with-error-on-month-input/preview) #### Markup
What is your date of birth? For example, 31 3 1980 Error message goes here
#### Macro {% from "date-input/macro.njk" import govukDateInput %} {{ govukDateInput({ "id": "dob-month-error", "namePrefix": "dob-month-error", "fieldset": { "legend": { "text": "What is your date of birth?" } }, "hint": { "text": "For example, 31 3 1980" }, "errorMessage": { "text": "Error message goes here" }, "items": [ { "name": "day", "classes": "govuk-input--width-2" }, { "name": "month", "classes": "govuk-input--width-2 govuk-input--error" }, { "name": "year", "classes": "govuk-input--width-4" } ] }) }} ### Date input with error on year input [Preview this example in the Frontend review app](http://govuk-frontend-review.herokuapp.com/components/date-input/with-error-on-year-input/preview) #### Markup
What is your date of birth? For example, 31 3 1980 Error message goes here
#### Macro {% from "date-input/macro.njk" import govukDateInput %} {{ govukDateInput({ "id": "dob-year-error", "namePrefix": "dob-year-error", "fieldset": { "legend": { "text": "What is your date of birth?" } }, "hint": { "text": "For example, 31 3 1980" }, "errorMessage": { "text": "Error message goes here" }, "items": [ { "name": "day", "classes": "govuk-input--width-2" }, { "name": "month", "classes": "govuk-input--width-2" }, { "name": "year", "classes": "govuk-input--width-4 govuk-input--error" } ] }) }} ### Date input with default items [Preview this example in the Frontend review app](http://govuk-frontend-review.herokuapp.com/components/date-input/with-default-items/preview) #### Markup
What is your date of birth? For example, 31 3 1980
#### Macro {% from "date-input/macro.njk" import govukDateInput %} {{ govukDateInput({ "id": "dob", "namePrefix": "dob", "fieldset": { "legend": { "text": "What is your date of birth?" } }, "hint": { "text": "For example, 31 3 1980" } }) }} ## Requirements ### Build tool configuration When compiling the Sass files you'll need to define includePaths to reference the node_modules directory. Below is a sample configuration using gulp .pipe(sass({ includePaths: 'node_modules/' })) ### Static asset path configuration In order to include the images used in the components, you need to configure your app to show these assets. Below is a sample configuration using Express js: app.use('/assets', express.static(path.join(__dirname, '/node_modules/govuk-frontend/assets'))) ## Component arguments If you are using Nunjucks,then macros take the following arguments **If you’re using Nunjucks macros in production be aware that using `html` arguments, or ones ending with `Html` can be a [security risk](https://en.wikipedia.org/wiki/Cross-site_scripting). More about it in the [Nunjucks documentation](https://mozilla.github.io/nunjucks/api.html#user-defined-templates-warning).**
Name Type Required Description
id string No Optional id. This is used for the main component and to compose id attribute for each item.
namePrefix string No Optional prefix. This is used to prefix each `item.name` using `-`.
items array Yes An array of input objects with name, value and optional classes
items.{}.id string No Optional item-specific id. If provided, it will be used instead of the generated id.
items.{}.name array Yes Item-specific name attribute.
items.{}.value string No Optional item-specific value attribute. If provided, it will be used as the initial value of the input.
items.{}.label string No Optional item-specific label text. If provided, this will be used instead of the items.{}.name.
hint object No Optional hint. See hint component.
errorMessage object No Optional error message. See errorMessage component.
fieldset object No Arguments for the fieldset component (e.g. legend). See fieldset component.
classes string No Optional additional classes to add to the date-input container.
attributes object No Any extra HTML attributes (for example data attributes) to add to the date-input container.
**If you’re using Nunjucks macros in production be aware that using `html` arguments, or ones ending with `Html` can be a [security risk](https://en.wikipedia.org/wiki/Cross-site_scripting). More about it in the [Nunjucks documentation](https://mozilla.github.io/nunjucks/api.html#user-defined-templates-warning).** ### Setting up Nunjucks views and paths Below is an example setup using express configure views: nunjucks.configure('node_modules/govuk-frontend/components', { autoescape: true, cache: false, express: app }) ## Contribution Guidelines can be found at [on our Github repository.](https://github.com/alphagov/govuk-frontend/blob/master/CONTRIBUTING.md "link to contributing guidelines on our github repository") ## License MIT