# Handling Illegal Dates When User Enters Date

**URL:** <https://www.ruby-forum.com/t/handling-illegal-dates-when-user-enters-date/127529>\
**Category:** Rails\
**Created:** [January 30, 2008, 2:46am UTC](https://www.ruby-forum.com/t/handling-illegal-dates-when-user-enters-date/127529 "2008-01-30T02:46:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [January 30, 2008, 2:46am UTC](https://www.ruby-forum.com/t/handling-illegal-dates-when-user-enters-date/127529/1 "2008-01-30T02:46:01Z")

</div>

For my website when a user creates an account they have to fill out  
their  
birthdate using pulldown menus. If a user enters an illegal date the  
system  
throws an error before I even get the opportunity to validate the date  
in  
the user model on save.

Soon as I do the following in my controller:

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

The following error is thrown:

1 error(s) on assignment of multiparameter attributes

How are people handling situations like this… what is the best  
practice?  
I don’t personaly want to do validation outside of the model… that  
would  
just suck :-p. Thanks for any input you might have!

Thanks :-).

–  
John K.  
removed\_email\_address@domain.invalid

Blog: [http://www.kopanas.com](http://www.kopanas.com)  
Conference: [http://www.cusec.net](http://www.cusec.net)  
Twits: [http://www.twitter.com/kopanas](http://www.twitter.com/kopanas)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [January 30, 2008, 5:15am UTC](https://www.ruby-forum.com/t/handling-illegal-dates-when-user-enters-date/127529/2 "2008-01-30T05:15:30Z")

</div>

Here’s what I’ve done:

begin  
@user = User.new(params[:user])  
rescue ActiveRecord::MultiparameterAssignmentErrors  
# date is invalid, so reset these fields before creating new  
user object  
params[:user][‘date\_of\_birth(1i)’] = ‘’  
params[:user][‘date\_of\_birth(2i)’] = ‘’  
params[:user][‘date\_of\_birth(3i)’] = ‘’  
# try again, letting our model’s  
validates\_presence\_of :date\_of\_birth  
# handle things.  
@user = User.new(params[:user])  
end

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [January 30, 2008, 5:16am UTC](https://www.ruby-forum.com/t/handling-illegal-dates-when-user-enters-date/127529/3 "2008-01-30T05:16:40Z")

</div>

By the way, I neglected to mention the fact that this code goes in  
your controller. Also, the formatting got messed up, but I think you  
can still get the gist of it.

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [January 30, 2008, 5:31am UTC](https://www.ruby-forum.com/t/handling-illegal-dates-when-user-enters-date/127529/4 "2008-01-30T05:31:21Z")

</div>

Your solution makes sense… but isn’t the problem with this that you  
are  
not adding an error onto your model’s error stack so a proper error is  
not  
being displayed? I am assuming their is more “Rails Way” of handling  
this… no?

On Jan 29, 2008 11:16 PM, Pat N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > rescue ActiveRecord::MultiparameterAssignmentErrors
> > 
> > > Soon as I do the following in my controller:  
> > > would  
> > > Twits:[http://www.twitter.com/kopanas](http://www.twitter.com/kopanas)

–  
John K.  
removed\_email\_address@domain.invalid

Blog: [http://www.kopanas.com](http://www.kopanas.com)  
Conference: [http://www.cusec.net](http://www.cusec.net)  
Twits: [http://www.twitter.com/kopanas](http://www.twitter.com/kopanas)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [January 30, 2008, 6:12am UTC](https://www.ruby-forum.com/t/handling-illegal-dates-when-user-enters-date/127529/5 "2008-01-30T06:12:57Z")

</div>

Good point. While it’s still controller centric, here’s another pass,  
with a specific error on the date\_of\_birth field:

begin  
@user = User.new(params[:user])  
rescue ActiveRecord::MultiparameterAssignmentErrors

# date is invalid, so reset these fields before creating new user

object  
params[:user][‘date\_of\_birth(1i)’] = ‘’  
params[:user][‘date\_of\_birth(2i)’] = ‘’  
params[:user][‘date\_of\_birth(3i)’] = ‘’

# try again, letting our model’s

validates\_presence\_of :date\_of\_birth

# handle things.

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

# Adding a specific error:

@user.errors.add(:date\_of\_birth, “was invalid”)  
end
