# Validation conundrum with Dates

**URL:** <https://www.ruby-forum.com/t/validation-conundrum-with-dates/71633>\
**Category:** Rails\
**Created:** [October 10, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/validation-conundrum-with-dates/71633 "2006-10-10T14:46:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jake\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4491bb/32.png) [@Jake\_C](https://www.ruby-forum.com/u/Jake_C)\
**Post date:** [October 10, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/validation-conundrum-with-dates/71633/1 "2006-10-10T14:46:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jake\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4491bb/32.png) [@Jake\_C](https://www.ruby-forum.com/u/Jake_C)\
**Post date:** [October 10, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/validation-conundrum-with-dates/71633/2 "2006-10-10T17:45:47Z")

</div>

how about using before\_type\_cast?

(untested):  
if !event\_date.nil? and event\_date\_before\_type\_cast !~  
/\d{4}-\d{1,2}-\d{1,2}/  
…

---

<div class="post-metadata">

**Author:** ![Jake\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4491bb/32.png) [@Jake\_C](https://www.ruby-forum.com/u/Jake_C)\
**Post date:** [October 10, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/validation-conundrum-with-dates/71633/3 "2006-10-10T18:05:52Z")

</div>

Jake C. wrote:

> 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?

Try using validates\_format\_of with the :if param maybe?

validates\_format\_of :event\_date, :with =\> /^\d{4}-\d{1,2}-\d{1,2}$/,  
:if =\> lambda {|date| not date.nil?}

---

<div class="post-metadata">

**Author:** ![Jake\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4491bb/32.png) [@Jake\_C](https://www.ruby-forum.com/u/Jake_C)\
**Post date:** [October 10, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/validation-conundrum-with-dates/71633/4 "2006-10-10T19:14:20Z")

</div>

On 10/10/06, jemminger [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> how about using before\_type\_cast?
> 
> (untested):  
> if !event\_date.nil? and event\_date\_before\_type\_cast !~  
> /\d{4}-\d{1,2}-\d{1,2}/

Aha! Nice…and here I was trying event\_date.before\_type\_cast…not  
event\_data\_before\_type\_cast!

Thanks! I think I can make this work!

Jake

---

<div class="post-metadata">

**Author:** ![Jake\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4491bb/32.png) [@Jake\_C](https://www.ruby-forum.com/u/Jake_C)\
**Post date:** [October 10, 2006, 7:42pm UTC](https://www.ruby-forum.com/t/validation-conundrum-with-dates/71633/5 "2006-10-10T19:42:28Z")

</div>

On 10/10/06, Jake C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks! I think I can make this work!

It indeed works perfectly…thank you very much. I beat my head on this  
deceptively simple thing for HOURS!!!

Best regards 😉

Jake

---

<div class="post-metadata">

**Author:** ![Jake\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4491bb/32.png) [@Jake\_C](https://www.ruby-forum.com/u/Jake_C)\
**Post date:** [October 11, 2006, 3:22pm UTC](https://www.ruby-forum.com/t/validation-conundrum-with-dates/71633/6 "2006-10-11T15:22:01Z")

</div>

This still won’t save you from dates such as 2006-02-30, and the like.

One common technique is to do something like:

def validate  
d = Date.new(your\_year\_field, your\_month\_field, your\_day\_field)  
rescue WhateverDateThrowsWhenItsNotValid =\> e  
do\_something\_with\_e.message  
end

On 10/10/06, Jake C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Jake
> 
> >

–  
If I can’t think of at least three different interpretations of what I  
received, I haven’t thought enough about what it might mean. – Jerry  
Weinberg
