My solution, after poking around a sparse documentation and the source code for calendar_date_select was to change the format on that end.

Rails DateTime Validation

For validating dates and times in your Rails application, pick up the handy gem validates_timeliness. It allows you to very easily ensure that dates are in specific ranges or that dates happen after or before other dates.

After you install the gem (gem install validates_timeliness) and have added it to your environment.rb (config.gem “validates_timeliness”), you are ready to rock. Remember to restart your server after editing environment.rb.

Here is a simple example I have used to make sure that the date is on or after today’s date:

If you look at the excellent documentation on Github, you will find many different options such as :before, :is_at, and :between.

The other gem I use with dates is calendar_date_select to allow a nice calendar interface to select dates. From my experience, these two don’t cooperate out-of-the-box. The calendar_date_select gem uses a date format that is not recognized by validates_timeliness. There is an option to change the accepted format in validates_date (or validates_time, validates_datetime), but that would not budge either.

My solution, after poking around a sparse documentation and the source code for calendar_date_select was to change the format on that end. All you need to is create an initializer named calendar_date_select.rb (or whatever you like, really) and use the following code:

That puts the date in the format that is by default accepted by validates_timeliness. Restart your server and you’re good to go.

Contact

Follow me on Twitter

Send me an email at godin.aaron@gmail.com.

Go up to the top or back home.