Date validation

Hello,

What is the best way to validate date periods? I have a model which has
two date attributes, and the user is not able to create a new record if
date1 is greater than date2. I tried using validates_numericality_of but
it doesn’t work with dates.

Eduardo B. wrote:
Hello,

What is the best way to validate date periods? I have a model which has
two date attributes, and the user is not able to create a new record if
date1 is greater than date2. I tried using validates_numericality_of but
it doesn’t work with dates.

I created my own validator for dates checking for the correct order and for valid dates. I check valid dates by creating and catchng the exception if the date is invalid.



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Create your own validation method like this:

http://guides.rubyonrails.org/activerecord_validations_callbacks.html#creating-custom-validation-methods

irb(main):001:0> require ‘date’
=> true

irb(main):002:0> d1 = Date.new(2009, 4, 1)
=> #<Date: 2009-04-01 (4909845/2,0,2299161)>

irb(main):003:0> d2 = Date.new(2009, 5, 1)
=> #<Date: 2009-05-01 (4909905/2,0,2299161)>

irb(main):004:0> d3 = Date.new(2009, 6, 1)
=> #<Date: 2009-06-01 (4909967/2,0,2299161)>

irb(main):005:0> d2.between? d1, d3
=> true

On Oct 16, 12:24 pm, “[email protected]