Hi guys.
I have a very interesting validation problem.
Say I have a model defined called Event, and this model has a field :event_date (for illustration, we could assume it’s the only field aside from id) that is defined as a Date type.
I want to allow this field to either be a valid date of a format of my choosing (\d{4}-\d{1,2}-\d{1,2}), or nil.
In other to validate, I need to use validates_format_of or a custom validate method. validates_format_of.
Since the field could already (and validly) be nil, I decide to implement a custom check:
class Event < ActiveRecord::Base def validate if !event_date.nil? and event_date !~ /\d{4}-\d{1,2}-\d{1,2}/
errors.add("event_date", "is not formatted properly")
end end
So far so good, right? It appears that, if the user set event_date to something, the regex will be tested and an error will be thrown if event_date doesn’t match it.
HOWEVER, before validate is called, the event_date value (after being set by the action from its parameters) appears to have already been cast to a Date, which causing a string that can’t be handled by Date to be set to nil. So, now our first test, event_date.nil? fails and the regex is never called…consequently, the user it not notified of the error. I have no way of getting at the parameter they passed here, so no way of knowing if they should indeed incur an error.
So…I need a boost. Can anyone tell me how they might accomplish this, forcing a date to either be nil or formatted properly, while indicating errors in formatting to the user?
Thanks a bunch! Jake