如何在rails 3上的ruby中轻松验证日期选择框?

时间:2011-06-24 16:57:52

标签: ruby-on-rails ruby ruby-on-rails-3 rubygems ruby-on-rails-plugins

validates_presence_of在我的日期字段中不起作用。有没有一种简单的方法来检查用户是否选择了日期(例如,生日)?

我在选择框中使用“Day:”,“Month:”和“Year:”的空白值。我想确保用户无法通过验证,除非他们选择日期。

 <%= 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:'
             } %>

1 个答案:

答案 0 :(得分:1)

您可能想要查看validates_timeliness gem。自述文件中的示例:

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'