README.markdown

validates_date_time

Install

Instructions

The validators can be used to parse strings into Date and Time objects as well as restrict
an attribute based on other dates or times.
class Person < ActiveRecord::Base
validates_date :date_of_birth
validates_time :time_of_birth
validates_date_time :date_and_time_of_birth
end

Use :allow_nil to allow the value to be blank.
class Person < ActiveRecord::Base
validates_date :date_of_birth, :allow_nil => true
end

Supported formats

The default for the plugin is to expect dates in day/month/year format. If you are in the
US, you will want to change the default to month/day/year by placing the following in config/environment.rb
ValidatesDateTime.us_date_format = true

In the final example, as I'm sure you are aware, the record failed to save not only
because "java is better than ruby" is an invalid date, but more importantly, because the statement is blatantly false. ;)

Restricting date and time ranges

Using the :before and :after options you can restrict a date or time value based on other attribute values
and predefined values. You can pass as many value to :before or :after as you like.
class Person
validates_date :date_of_birth, :before => [:date_of_death, Proc.new { 1.day.from_now_to_date}], :after => '1 Jan 1900'
validates_date :date_of_death, :before => Proc.new { 1.day.from_now.to_date }
end

You can customise the error messages for dates or times that fall outside the required range. The boundary date will be substituted in for %s. Eg:
class Person
validates_date :date_of_birth, :after => Date.new(1900, 1, 1), :before => Proc.new { 1.day.from_now.to_date }, :before_message => 'Ensure it is before %s', :after_message => 'Ensure it is after %s'
end