开发者

How to easily validate date select boxes in ruby on rails 3?

validates_presence_of doesn't work on my date field. Is there a s开发者_开发问答imple 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:'
             } %>


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'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜