Date input
Date inputs allow the user to enter the day, month and year of an event.
The date input’s parameters match those used by Rails’
date_select
helper. To access the parameters in your controller
you should just need to permit the attribute, for example:
# in UsersController
def user_params
params.require(:user).permit(:date_of_birth, ...)
end
Three inputs for day, month and year respectively are used to capture the dates rather than a date picker. This is because:
- some users find date pickers difficult to use
- often users will be expected to enter a date they know rather than to pick a date
Exact date input with legend and hint and birth date autocompletion
Approximate dates – recording the closest month
When asking for dates that the user might not know or could struggle to
remember, such as their graduation date, the exact date might not be
necessary. The day can be optionally omitted by providing the
omit_day
parameter.
When mapped to a Date
attribute, Ruby will automatically
set the Day
to be 1
when none is provided. This
should be taken into account when displaying the data or using it for
analysis.
Specifying client side length restrictions on date inputs with maxlength
To enable client side restrictions on the number of characters each
input requires, you can use the maxlength_enabled
parameter.
This will default the day and month inputs to 2 characters and the year
input to 4 characters.