I want to validate a date in my model in Ruby on Rails, however, the day, month and year values are already converted into an incorrect date by the time they reach my model.
For example, if I enter February 31st 2009 in my view, when I use Model.new(params[:model])
in my controller, it converts it to "March 3rd 2009", which my model then sees as a valid date, which it is, but it is incorrect.
I would like to be able to do this validation in my model. Is there any way that I can, or am I going about this completely wrong?
I found this "Date validation" that discusses the problem but it never was resolved.
A bit late here, but thanks to "How do I validate a date in rails?" I managed to write this validator, hope is useful to somebody:
Inside your
model.rb
If you want Rails 3 or Ruby 1.9 compatibility try the date_validator gem.
You can validate the date and time like so (in a method somewhere in your controller with access to your params if you are using custom selects) ...