1

validates_presence_of doesn't work on my date field. Is there a simple way to check that user has selected a date (a birthday, for instance)?

I'm using blank values of "Day:", "Month:", and "Year:" on my select boxes. I want to make sure that a user can't pass validation unless they select a date.

 <%= f.input :birthday,
             :label      => 'Birthday:',
             :as         => :date,
             :start_year => Date.current.year,
             :end_year   => Date.current.year - 106,
             :order      => [:day, :month, :year],
             :prompt     => {
               :day   => 'Day:',
               :month => 'Month:',
               :year  => 'Year:'
             } %>
theIV
  • 25,434
  • 5
  • 54
  • 58
LondonGuy
  • 10,778
  • 11
  • 79
  • 151
  • [This answer about date validation](http://stackoverflow.com/questions/597328/how-do-i-validate-a-date-in-rails) may fit your wants – Adrien Coquio Jun 24 '11 at 18:37

1 Answers1

1

You might want to have a look at the validates_timeliness gem. Examples from the Readme:

validates_datetime :occurred_at

validates_date :date_of_birth, :before => lambda { 18.years.ago },
                            :before_message => "must be at least 18 years old"

validates_datetime :finish_time, :after => :start_time # Method symbol

validates_date :booked_at, :on => :create, :on_or_after => :today # See Restriction Shorthand.
validates_time :booked_at, :between => ['9.00am', '5:00pm']

validates_time :breakfast_time, :on_or_after => '6:00am',
                              :on_or_after_message => 'must be after opening time',
                              :before => :lunchtime,
                              :before_message => 'must be before lunch time'
KenB
  • 6,587
  • 2
  • 35
  • 31
  • Tried using the validates_timeliness gem and it has no effect. I have blank values set up and when I select nothing it makes no difference. No error message. All my other error checking is fine but when it comes to this date field I get errors. – LondonGuy Jun 24 '11 at 21:08
  • Did you include :allow_blank => false in your setup? – KenB Jun 24 '11 at 21:37