# Date validation

**URL:** <https://www.ruby-forum.com/t/date-validation/99001>\
**Category:** Rails\
**Created:** [January 30, 2006, 8:10pm UTC](https://www.ruby-forum.com/t/date-validation/99001 "2006-01-30T20:10:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [January 30, 2006, 8:10pm UTC](https://www.ruby-forum.com/t/date-validation/99001/1 "2006-01-30T20:10:26Z")

</div>

Has anyone ever tweaked the standard date controls to ban dates like  
30th february ?

As standard, if you enter 30th february , it goes through quietly (no  
errors) as 2nd march.

I’ve rummaged around in the source code, but so far I’ve been unable to  
discover the process that magically transforms the six separate bits of  
a form date-time field back into a single date when the form is  
submitted. I’d guess that finding that process is key to fixing the  
problem.

TIA, Andy

---

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [January 30, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/date-validation/99001/2 "2006-01-30T21:46:53Z")

</div>

I just use the standard library’s date validation  
within the model’s validate method.

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [January 30, 2006, 11:42pm UTC](https://www.ruby-forum.com/t/date-validation/99001/3 "2006-01-30T23:42:14Z")

</div>

rails nut writes:

> Has anyone ever tweaked the standard date controls to ban dates like  
> 30th february ?
> 
> As standard, if you enter 30th february , it goes through quietly (no  
> errors) as 2nd march.

Tom M. \<tmornini@…\> writes:

> I just use the standard library’s date validation  
> within the model’s validate method.

If Rails automatically converts a ‘bad’ date into it’s equivalent ‘good’  
date, wouldn’t the model’s validation method always see a valid date?  
At what  
point in the object’s life cycle does ‘Feb 30’ or ‘June 31’ get  
translated  
into ‘March 2’ and ‘July 1’?

> From the descriptions, it seems that rails nut’s app is happily chugging along  
> when weird dates are entered, while Tom’s models raise a validation  
> error. I’m  
> assuming, of course, that if rails nut added date validation to the  
> method, it  
> would also barf on a weird date, meaning that validation is the way to  
> handle  
> this issue. However, if the bad dates go through even with validation,  
> then  
> that is something different…

…Isn’t it? I’m a n00b t00, so this is more of a “verifiying my  
knowledge”  
than a “this is the answer.”

---

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [January 31, 2006, 12:59am UTC](https://www.ruby-forum.com/t/date-validation/99001/4 "2006-01-31T00:59:34Z")

</div>

To clarify what’s happening:

User has a form to create new object which has scaffolded code for a  
date field. User selects feb 30th and hits submit on the form. New  
object is created by rails with no validation errors, but the date comes  
back as march 2nd.

I would like feb 30th to raise an error somehow, preferably in  
validation at the model layer, but I haven’t yet found out how to do  
date validation.

Andy

---

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [January 30, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/date-validation/99001/5 "2006-01-30T21:46:56Z")

</div>

Open up ActiveRecord::Base and look for the attribute= method. Its  
starts  
there.

Bob

---

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [January 31, 2006, 11:20am UTC](https://www.ruby-forum.com/t/date-validation/99001/6 "2006-01-31T11:20:28Z")

</div>

Tom M. wrote:

> I just use the standard library’s date validation  
> within the model’s validate method.
> 
> –  
> – Tom M.

I can’t see anything for date validation in the rails book or the ruby  
book. Can you tell me what you mean by ‘standard library date  
validation’ please ?

Andy

---

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [February 1, 2006, 5:59am UTC](https://www.ruby-forum.com/t/date-validation/99001/7 "2006-02-01T05:59:20Z")

</div>

On Jan 31, 2006, at 2:20 AM, rails nut wrote:

> Tom M. wrote:
> 
> > I just use the standard library’s date validation  
> > within the model’s validate method.
> > 
> > –  
> > – Tom M.
> 
> I can’t see anything for date validation in the rails book or the ruby  
> book. Can you tell me what you mean by ‘standard library date  
> validation’ please ?

Sorry for the long delay on this.

The talk of Rails returning dates that don’t match the  
input rather surprised me. I’ve been working on tests  
and backend work almost exclusively, so I wanted to  
investigate this for myself completely before discussing  
it further.

OK, I have a form that allows date input, and on submit  
I get this in the logs:

Parameters: {“buyer”=\>{“born\_on(1i)”=\>“1988”,“born\_on  
(2i)”=\>“2”,“born\_on(3i)”=\>“31”}, “commit”=\>“Create”}

I can access the buyer.born\_on field, as you’ve been discussing, but  
I can also access:

params[:buyer][‘born\_on(1i)’] -\> 1988  
params[:buyer][‘born\_on(2i)’] -\> 2  
params[:buyer][‘born\_on(3i)’] -\> 31

Which can be tested with this application.rb method:

def test\_date(object,attribute)  
Date.valid\_civil?(params[object][attribute + ‘(1i)’].to\_i,  
params[object][attribute + ‘(2i)’].to\_i,  
params[object][attribute + ‘(3i)’].to\_i)  
end

by writing:

def valid  
test\_date(:buyer,‘born\_on’)  
end

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [February 1, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/date-validation/99001/8 "2006-02-01T16:46:13Z")

</div>

Tom M. wrote:  
…

> Which can be tested with this application.rb method:
> 
> def test\_date(object,attribute)  
> Date.valid\_civil?(params[object][attribute + ‘(1i)’].to\_i,  
> params[object][attribute + ‘(2i)’].to\_i,  
> params[object][attribute + ‘(3i)’].to\_i)  
> end
> 
> by writing:
> 
> def valid  
> test\_date(:buyer,‘born\_on’)  
> end
> 
> –  
> – Tom M.

That’s pretty close to what I did in the end, except your code  
parameterises the field name to be checked, and I manually wrote the  
bans on feb 30 etc ('cos I didn’t know date.valid\_civil? existed).

I will probably re-factor based on your code … many thanks.

Andy

---

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [June 20, 2008, 8:07pm UTC](https://www.ruby-forum.com/t/date-validation/99001/9 "2008-06-20T20:07:28Z")

</div>

Was there any ever resolution with this? I am having the same exact  
problem and would really prefer to not have to put validation logic in  
my controller.

Sean

---

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [June 7, 2006, 1:49pm UTC](https://www.ruby-forum.com/t/date-validation/99001/10 "2006-06-07T13:49:43Z")

</div>

But that only lets you validate the date at the controller level. What  
if you want to perform validation at the model level, and from what I  
gather is the Rails way of doing things?

The point the previous posters have been making is that by the time the  
date is available for validation in the model it has already been  
coerced into a valid format by rails so any attempt to validate it will  
succeed.

I’ve put together a simple form that contains among other things a  
date\_select:

\<%= date\_select(:user, :dob, :order =\> [:day, :month, :year],  
:start\_year =\> 1900, :end\_year =\> Date.today.year) %\>

In my controller I have some code to create a user entity from the  
parameters posted up from the form, like so:

parameters: {“user”=\>{“dob(1i)”=\>“2006”, “dob(2i)”=\>“2”,  
“dob(3i)”=\>“31”}}

@user = User.new(params[:user])

As if by magic Rails will convert dates like 31/02/2006 into 03/03/2006  
so at the model level ‘user.dob’ =\> ‘03/03/2006’.

Are you supposed to refrain from constructing model objects using the  
above technique when dates are involved?

regards,  
Cathal.

Tom M. wrote:

> On Jan 31, 2006, at 2:20 AM, rails nut wrote:
> 
> > Tom M. wrote:
> > 
> > > I just use the standard library’s date validation  
> > > within the model’s validate method.
> > > 
> > > –  
> > > – Tom M.
> > 
> > I can’t see anything for date validation in the rails book or the ruby  
> > book. Can you tell me what you mean by ‘standard library date  
> > validation’ please ?
> 
> Sorry for the long delay on this.
> 
> The talk of Rails returning dates that don’t match the  
> input rather surprised me. I’ve been working on tests  
> and backend work almost exclusively, so I wanted to  
> investigate this for myself completely before discussing  
> it further.
> 
> OK, I have a form that allows date input, and on submit  
> I get this in the logs:
> 
> Parameters: {“buyer”=\>{“born\_on(1i)”=\>“1988”,“born\_on  
> (2i)”=\>“2”,“born\_on(3i)”=\>“31”}, “commit”=\>“Create”}
> 
> I can access the buyer.born\_on field, as you’ve been discussing, but  
> I can also access:
> 
> params[:buyer][‘born\_on(1i)’] -\> 1988  
> params[:buyer][‘born\_on(2i)’] -\> 2  
> params[:buyer][‘born\_on(3i)’] -\> 31
> 
> Which can be tested with this application.rb method:
> 
> def test\_date(object,attribute)  
> Date.valid\_civil?(params[object][attribute + ‘(1i)’].to\_i,  
> params[object][attribute + ‘(2i)’].to\_i,  
> params[object][attribute + ‘(3i)’].to\_i)  
> end
> 
> by writing:
> 
> def valid  
> test\_date(:buyer,‘born\_on’)  
> end
> 
> –  
> – Tom M.
