I've been trying to work this date_select form field helper in my Rails 4 app to be required but I have had no success.
<div class="field">
<%= f.label :birthday, "Your Date of Birth" %><br />
<%= f.date_select :birthday, order: [:month, :day, :year], prompt: { day: 'Select day', month: 'Select month', year: 'Select year' }, start_year: Date.today.year - 18, end_year: Date.today.year - 100 %>
</div>
I've done it successfully with text and password fields using (:required => true), for example:
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off", :required => true %>
</div>
But I'm unable to translate the required parameter setting to the date_select form field. I have tried :required => true, and required: true as well, but no success. And I have been unable to find any answers concerning this topic on Google and StackOverflow.
I'd really like to work this out with a user providing their birthday data instead of setting a default date if the form is submitted with nil information.
Thank you very much for your time.
Reez
The required is a html option so it's a separate hash from the other options. Use curly braces to surround the first hash so that the required is clearly in the second hash.
<%= f.date_select :birthday, {order: [:month, :day, :year], prompt: { day: 'Select day', month: 'Select month', year: 'Select year' }, start_year: Date.today.year - 18, end_year: Date.today.year - 100}, {required: true} %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With