开发者

Date validation helper for mongoid/active model?

are there any date validation helper for mongoid or active model? I want to check dates on valid ISO_8601 standard (http://en.wikipedia.org/wiki/ISO_8601#D开发者_开发百科ates) which is not a big deal with mongoids validates_format_of. But the date should be in future or depending on the field >= another date (start- and enddate).

My approach would be to check the format with validates_format_of and to write my own date validators for my further requirements.

Anyone done that before?

Thanks, Julian


your approach seems like a good one. the best way to do this is to break out the validation to its own function.

you can do this like so:

class Foo
   include Mongoid::Document
   field :date_time, :type => DateTime
   validate :date_is_ok?

   def date_is_ok
     unless self.date_time.to_i > Time.now.to_i
        errors.add :date_time, "must be in the future"
        return false
     end
     true
   end
end


Yes, validates_timeliness does complete validation of dates, times and datetimes for Rails 3.x and ActiveModel.

It explicitly supports Mongoid:

ValidatesTimeliness.setup do |config|

  # Extend ORM/ODMs for full support (:active_record, :mongoid).
  config.extend_orms = [ :mongoid ]

end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜