# How to validate a date

**URL:** <https://www.ruby-forum.com/t/how-to-validate-a-date/51934>\
**Category:** Rails\
**Created:** [December 3, 2005, 11:09pm UTC](https://www.ruby-forum.com/t/how-to-validate-a-date/51934 "2005-12-03T23:09:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicksnels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/aca169/32.png) [@nicksnels](https://www.ruby-forum.com/u/nicksnels)\
**Post date:** [December 3, 2005, 11:09pm UTC](https://www.ruby-forum.com/t/how-to-validate-a-date/51934/1 "2005-12-03T23:09:05Z")

</div>

Hi,

I would like to validate a date (29 feb 2005 is invalid) and if it is an  
invalid date display an error to the user, using the default way of  
displaying errors. In my view I have a simple date\_select:

\<%= date\_select (:listing, :start\_date,  
{:order =\> [:day, :month, :year],  
:start\_year =\> Time.now.year,  
:end\_year =\> Time.now.year+1}) %\>

When I save data to the db, the field start\_date should be validated and  
if their is an error display it. So in my model I have tried:

def validate\_on\_create  
year = start\_date(1i)  
month = start\_date(2i)  
day = start\_date(3i)  
unless Date::valid\_date?(year.to\_i, month.to\_i, day.to\_i)  
errors.add(:start\_date, “De startdatum moet een geldige datum  
zijn.”)  
end  
end

But this doesn’t work, because I can’t access the values inside  
start\_date in the model. I get errors like that start\_date is  
multiparameter . I know that, but I can’t get to the values of the  
parameters. Hope somebody can help.

Kind regards,

Nick

---

<div class="post-metadata">

**Author:** ![nicksnels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/aca169/32.png) [@nicksnels](https://www.ruby-forum.com/u/nicksnels)\
**Post date:** [December 3, 2005, 11:44pm UTC](https://www.ruby-forum.com/t/how-to-validate-a-date/51934/2 "2005-12-03T23:44:17Z")

</div>

Nick S. wrote:

> end  
> end
> 
> But this doesn’t work, because I can’t access the values inside  
> start\_date in the model. I get errors like that start\_date is  
> multiparameter . I know that, but I can’t get to the values of the  
> parameters. Hope somebody can help.

Nick, applying posted parameters to your model using new, attributes=,  
update\_attributes, etc., will automatically set start\_date to an  
object of type Date based on the three component parameters. If the  
date is invalid I think start\_date will be set to nil, for which you  
can check. If that’s not enough control, you’ll have to either arrange  
to give validate\_on\_create access to the params hash so you can check  
the values of the start\_date(i) keys, or do the validation in a  
separate date validation method called from the controller.

I wonder what the value of start\_date.before\_type\_cast would be?  
It’d be nice if this was a three-element array for easy validation.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![nicksnels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/aca169/32.png) [@nicksnels](https://www.ruby-forum.com/u/nicksnels)\
**Post date:** [December 3, 2005, 11:52pm UTC](https://www.ruby-forum.com/t/how-to-validate-a-date/51934/3 "2005-12-03T23:52:31Z")

</div>

On Dec 3, 2005, at 2:09 PM, Nick S. wrote:

> ```
> :end_year => Time.now.year+1}) %>
> errors.add(:start_date, "De startdatum moet een geldige datum
> 
> ```
> 
> Nick

I don’t know if this helps, but we’re validating the expiration date  
of a credit card this way:

require ‘Date’

validates\_each :payment\_mm do |m, a, month|  
if ((month.to\_i \< DateTime.now.month) && (m.payment\_yyyy.to\_i ==  
DateTime.now.year))  
m.errors.add(a, “Your credit card seems to be expired. Please  
enter valid payment information.”)  
end  
end

Also, remember that if you operate on an instance’s values, you need  
to refer to ‘self’. E.g., in the same model we send the order email  
and then clear the credit card number before saving:

def before\_save  
OrderMailer.deliver\_order(self)  
self.payment\_account="(not recorded)"  
end

Phil

---

<div class="post-metadata">

**Author:** ![nicksnels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/aca169/32.png) [@nicksnels](https://www.ruby-forum.com/u/nicksnels)\
**Post date:** [December 4, 2005, 2:23pm UTC](https://www.ruby-forum.com/t/how-to-validate-a-date/51934/4 "2005-12-04T14:23:44Z")

</div>

Hi,

thank you both for the command. Helped to clear some issues up. I have  
simplified my model code to, just to see the effect of the date:

def validate  
logger.warn ("Start-- " + start\_date.to\_s)  
end

I get a date back when I enter a valid date. But when I enter an invalid  
date I get the error:

1 error(s) on assignment of multiparameter attributes

I have tried to catch the error like this:

def validate  
begin  
unless self.start\_date  
errors.add(:start\_date, “De startdatum moet een geldige datum  
zijn.”)  
end  
rescue  
errors.add(:start\_date, “De startdatum moet een geldige datum zijn.”)  
end  
end

But I keep getting the error. The error obviously means the date I  
entered cann’t get converted to a valid date. So if I can catch this  
error I’m done. But …

Kind regards,

Nick

---

<div class="post-metadata">

**Author:** ![nicksnels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/aca169/32.png) [@nicksnels](https://www.ruby-forum.com/u/nicksnels)\
**Post date:** [December 4, 2005, 6:17pm UTC](https://www.ruby-forum.com/t/how-to-validate-a-date/51934/5 "2005-12-04T18:17:23Z")

</div>

Getting tired of all the error messages, I thought I would throw the  
date\_select away in favor of:

\<%= select\_day (Time.now.day, :prefix =\> “listing”) %\>  
\<%= select\_month (Time.now.month, :prefix =\> “listing”) %\>  
\<%= select\_year (Time.now.year, :prefix =\> “listing”, :start\_year =\>  
Time.now.year,  
:end\_year =\> Time.now.year+1) %\>

Generates the same HTML code and now I get three separate params, not  
related to each other, so no more multiparameter stuff.

I my model I added:

attr\_accessor :day, :month, :year

def validate  
if Date::valid\_civil?(year.to\_i, month.to\_i, day.to\_i) == nil  
errors.add(:year, “geen geldige datum.”)  
else  
self.start\_date = year.to\_s + “-” + month.to\_s + “-” + day.to\_s  
end  
end

So now I should also be able to check if the date is before today. Only  
problem I have right now is I can’t get Rails to draw a red box around  
my three select fields (day, month and year). But if that is all, at  
least my visitors are no longer able to enter an invalid date.

My advice stay away from date\_select if you want to do some serious date  
checking, at least until they introduce a decent date validator, like  
validate\_date\_of or something like that.

Kind regards,

Nick

---

<div class="post-metadata">

**Author:** ![nicksnels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/aca169/32.png) [@nicksnels](https://www.ruby-forum.com/u/nicksnels)\
**Post date:** [December 4, 2005, 8:15pm UTC](https://www.ruby-forum.com/t/how-to-validate-a-date/51934/6 "2005-12-04T20:15:48Z")

</div>

On Sat, 3 Dec 2005, Nick S. wrote:

> their is an error display it. So in my model I have tried:
> 
> But this doesn’t work, because I can’t access the values inside start\_date  
> in the model. I get errors like that start\_date is multiparameter . I know  
> that, but I can’t get to the values of the parameters. Hope somebody can  
> help.

say you have a table like:

create table moments (  
id serial,  
moment timestamp  
);

and a controller like this:

[ahoward@localhost date\_validation]$ cat  
app/controllers/moment\_controller.rb  
class MomentController \< ApplicationController  
scaffold :moment  
end

then a model like this will solve your problem

[ahoward@localhost date\_validation]$ cat app/models/moment.rb  
class ::Time  
class \<\< self  
def normalized? t, values  
tvalues = %w(year month day hour min sec usec).map{|m| t.send  
m}  
values.zip(tvalues){|vin,vout| return true if vin != vout}  
return false  
end  
%w( local mktime gm utc ).each do |method|  
eval \<\<-code  
alias _org#{ method }_\_ #{ method }  
def #{ method }(\*a, &b)  
t = _org#{ method }_\_(\*a, &b)  
t.instance\_eval{@original\_arguments = a}  
t.instance\_eval{@normalized = self.class.normalized? self,  
a}  
t  
end  
code  
end  
end  
attr\_reader ‘normalized’  
alias\_method ‘normalized?’, ‘normalized’  
attr\_reader ‘original\_arguments’  
end

class Moment \< ActiveRecord::Base  
protected  
def validate \*a, &b  
t = moment  
if t.nil? or t.normalized?  
values = t.original\_arguments  
errors.add self.class.to\_s, “bad time from  
\<#{values.inspect}\>”  
end  
super  
end  
end

though you’d want to put the Time code somewhere else in practice…

an attempt to enter Feb 31st on my system results in an error page that  
looks like

```
 Editing moment
 1 error prohibited this moment from being saved

 There were problems with the following fields:

     * Moment bad time from <[2005, 2, 31, 11, 48]>

```

i’ve posted this fix in the past but, for some reason, there was not  
much  
interest. read this thread for more info:

[http://wrath.rubyonrails.org/pipermail/rails/2005-March/004619.html](http://wrath.rubyonrails.org/pipermail/rails/2005-March/004619.html)

this technique could easily be used to make a nice time/date validator  
and is,  
in fact, extensible for any multi-param rails object: the approach is  
always  
the same - try ctor, munge inputs if required, remember the original  
arguments  
for later access in validation.

kind regards.

## -a

# =============================================================================== | ara [dot] t [dot] howard [at] noaa [dot] gov | all happiness comes from the desire for others to be happy. all misery | comes from the desire for oneself to be happy. | – bodhicaryavatara

---

<div class="post-metadata">

**Author:** ![nicksnels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/aca169/32.png) [@nicksnels](https://www.ruby-forum.com/u/nicksnels)\
**Post date:** [December 5, 2005, 1:48am UTC](https://www.ruby-forum.com/t/how-to-validate-a-date/51934/7 "2005-12-05T01:48:53Z")

</div>

removed\_email\_address@domain.invalid wrote:

> On Sat, 3 Dec 2005, Nick S. wrote:
> 
> > I would like to validate a date (29 feb 2005 is invalid) and if it is an  
> > invalid date display an error to the user, using the default way of  
> > displaying errors. In my view I have a simple date\_select:

[snip]

> app/controllers/moment\_controller.rb  
> class MomentController \< ApplicationController  
> scaffold :moment  
> end
> 
> then a model like this will solve your problem

[snip]

> arguments  
> for later access in validation.
> 
> kind regards.
> 
> -a

Hi, Ara - having discussed this with you over eight months ago, I would  
never have believed that Rails would reach 1.0 without sorting it out.  
Maybe date\_select, like scaffolding, isn’t intended for production use.

regards

Justin

---

<div class="post-metadata">

**Author:** ![nicksnels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/aca169/32.png) [@nicksnels](https://www.ruby-forum.com/u/nicksnels)\
**Post date:** [December 4, 2005, 8:48pm UTC](https://www.ruby-forum.com/t/how-to-validate-a-date/51934/8 "2005-12-04T20:48:23Z")

</div>

— Nick S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> {:order => [:day, :month, :year],
> 
> ```
> 
> month = start\_date(2i)  
> values inside  
> start\_date in the model. I get errors like that  
> start\_date is  
> multiparameter . I know that, but I can’t get to the  
> values of the

I made a date validator:  
[Oh, No. He’s getting organized](http://www.garbett.org/?q=node/27), as well as several  
other helpers. This code is free for the taking. No  
license just pure public domain. I relinquish all  
copyrights and all that other legal stuff on this  
code. May it help you.

With this one can just put  
validates\_date :birthdate

in the model, and the problem is solved.

Code from site:

require ‘parsedate’

ActiveRecord::Base.class\_eval do

def self.validate\_date(string)  
data = ParseDate.parsedate(string)  
0.upto(2) { |i| raise “Bogus date [#{string}]  
encountered” if data[i].nil? }  
raise “Pre-1800 date [#{string}] encountered” if  
data[0] \< 1800  
raise “Invalid date [#{string}] encountered” if  
nil==Date.new(data[0],data[1],data[2])  
return true  
rescue Exception =\> e  
logger.error(“Date format exception [#{e}]”)  
return false  
end

# Configuration options:

# \* message - A custom error message (default is:

“can’t be blank”)

# \* on - Specifies when this validation is active

(default is :save, other options :create, :update)

# \* if - Specifies a method, proc or string to call

to determine if the validation should

# occur (e.g. :if =\> :allow\_validation, or :if =\>

Proc.new { |user| user.signup\_step \> 2 }). The

# method, proc or string should return or evaluate

to a true or false value.  
def self.validates\_date(\*attr\_names)  
configuration = { :message =\> ‘Invalid date, use  
(MM/DD/YYYY)’, :on =\> :save }  
configuration.update(attr\_names.pop) if  
attr\_names.last.is\_a?(Hash)  
attr\_names.each do |attr\_name|  
send(validation\_method(configuration[:on])) do  
|record|  
unless configuration[:if] &&  
!evaluate\_condition(configuration[:if], record)  
date =  
record.attributes\_before\_type\_cast[attr\_name.to\_s]  
if not date.nil? and not date.class == Date  
and date.length \> 0 and date.class == String and not  
self.validate\_date(date)

record.errors.add(attr\_name,configuration[:message])  
end  
end  
end  
end  
end  
end

* * *

Do You Yahoo!?  
Tired of spam? Yahoo! Mail has the best spam protection around

> **[\[object](https://mail.yahoo.com/)**
>
> \[object

---

<div class="post-metadata">

**Author:** ![nicksnels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/aca169/32.png) [@nicksnels](https://www.ruby-forum.com/u/nicksnels)\
**Post date:** [December 5, 2005, 1:52am UTC](https://www.ruby-forum.com/t/how-to-validate-a-date/51934/9 "2005-12-05T01:52:59Z")

</div>

On Mon, 5 Dec 2005, Justin F. wrote:

> Hi, Ara - having discussed this with you over eight months ago, I would  
> never have believed that Rails would reach 1.0 without sorting it out. Maybe  
> date\_select, like scaffolding, isn’t intended for production use.

indeed. particularly when there are several viable alternatives that  
address  
the issue floating around. i’m not going to admit in public that i only  
remember i’d solved this once before until **after** searching the  
archives  
and stubmling on the thread between you and i 😉

cheers.

## -a

# =============================================================================== | ara [dot] t [dot] howard [at] noaa [dot] gov | all happiness comes from the desire for others to be happy. all misery | comes from the desire for oneself to be happy. | – bodhicaryavatara
