How do I validate a date in rails? How do I validate a date in rails? ruby ruby

How do I validate a date in rails?


I'm guessing you're using the date_select helper to generate the tags for the date. Another way you could do it is to use select form helper for the day, month, year fields. Like this (example I used is the created_at date field):

<%= f.select :month, (1..12).to_a, selected: @user.created_at.month %><%= f.select :day, (1..31).to_a, selected: @user.created_at.day %><%= f.select :year, ((Time.now.year - 20)..Time.now.year).to_a, selected: @user.created_at.year %>

And in the model, you validate the date:

attr_accessor :month, :day, :yearvalidate :validate_created_atprivatedef convert_created_at  begin    self.created_at = Date.civil(self.year.to_i, self.month.to_i, self.day.to_i)  rescue ArgumentError    false  endenddef validate_created_at  errors.add("Created at date", "is invalid.") unless convert_created_atend

If you're looking for a plugin solution, I'd checkout the validates_timeliness plugin. It works like this (from the github page):

class Person < ActiveRecord::Base  validates_date :date_of_birth, on_or_before: lambda { Date.current }  # or  validates :date_of_birth, timeliness: { on_or_before: lambda { Date.current }, type: :date }end 

The list of validation methods available are as follows:

validates_date     - validate value as datevalidates_time     - validate value as time only i.e. '12:20pm'validates_datetime - validate value as a full date and timevalidates          - use the :timeliness key and set the type in the hash.


Using the chronic gem:

class MyModel < ActiveRecord::Base  validate :valid_date?  def valid_date?    unless Chronic.parse(from_date)      errors.add(:from_date, "is missing or invalid")    end  endend


If you want Rails 3 or Ruby 1.9 compatibility try the date_validator gem.