Tag Archive | date_select

rails date validation, rails validate date for date_select

Ok, you are in right place if you need to validate a date from date_select helper.
Expecially if your problem is an invalid date like ’31/02/2009′  that becomes ’02/03/2009′ on rails!
If you don’t want to waste your time read here and don’t go immediatly to rails date validation plugin .

validates_timeliness plugin introduces a lot of great features to validate a date but its documentation is not so cool (this is my absolutely wrong opinion). You have to read it completly or take a look here about 3 important points:

  • I18n
    if you don’t use :en locale you have to implement validates_timeliness default translation in your translations file (for ex. config/locales/it.yml) or you will waste your time to debug stupid exceptions. Default plugin translation file can be found in vendor/plugin/validates_timeliness/lib/validates_timeliness/locale after installation;
  • Validate invalid date (like 31/02/2009 becomes 02/03/09) from select helpers
    You need to know that validates_timeliness doesn’t fix this problem automatically as you expect!
    You have to manually enable validates_timeliness workaround if you need to validate invalid date on rails.
    Put ” ValidatesTimeliness.enable_datetime_select_extension! ” your environment.rb (EOF) to enable validate_timeliness workaround and catch bad dates.
    Note: Patch to ticket #10556 introduced this problem: rails accepts invalid dates from input (instantiate Time object and convert it back to a date).  This problem generates a lot of confusion, checks on controllers, horrible workarounds and so on… A valid reason seems to exist! But WTF!!! People in panic and code with consistency problems…

Any other information (and download) can be find on validates_timeliness at github.

Have a nice day!