Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 How Do You Make 'date_select' Form Field Helper Required?

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

like image 768
Reezy Avatar asked Dec 11 '22 23:12

Reezy


1 Answers

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} %>
like image 159
SteveTurczyn Avatar answered Apr 09 '23 18:04

SteveTurczyn