# How do you validate dates of models

**URL:** <https://www.ruby-forum.com/t/how-do-you-validate-dates-of-models/123669>\
**Category:** Rails\
**Created:** [December 19, 2007, 4:50pm UTC](https://www.ruby-forum.com/t/how-do-you-validate-dates-of-models/123669 "2007-12-19T16:50:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [December 19, 2007, 4:50pm UTC](https://www.ruby-forum.com/t/how-do-you-validate-dates-of-models/123669/1 "2007-12-19T16:50:29Z")

</div>

i am wondering how you deal with date validation in your models … if i  
have a date attribute it only eats date objects … so i when i do this

u = User.new(:born =\> Date.today)

it works, but this results in a nil value in :born attribute …

u = User.new(:born =\> ‘10.10.2007’)  
puts(u.born) #results in nil

so now i cannot really validate dates because the string is not stored  
in the attribute … whereas all other attributes store everything …  
when i excpect a number in an attribute i can assign a string but the  
object will stay invalid (if i have a validator)

so whats the best thing to deal with dates in connection with a web  
form?

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [December 20, 2007, 12:30am UTC](https://www.ruby-forum.com/t/how-do-you-validate-dates-of-models/123669/2 "2007-12-20T00:30:46Z")

</div>

You need to pass it a Date object all the time. Something like  
10-10-2007 or  
10/10/2007 should work.

“but this results in a nil value in :born attribute …”

The above confused me. Did you do  
u = User.new(:born =\> Date.today)  
u.born

only for it to return nil?  
On Dec 20, 2007 2:20 AM, Michal G. \<  
removed\_email\_address@domain.invalid\> wrote:

> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [December 20, 2007, 2:12am UTC](https://www.ruby-forum.com/t/how-do-you-validate-dates-of-models/123669/3 "2007-12-20T02:12:54Z")

</div>

Michal G. wrote:

> i am wondering how you deal with date validation in your models … if i  
> have a date attribute it only eats date objects … so i when i do this
> 
> u = User.new(:born =\> Date.today)
> 
> it works, but this results in a nil value in :born attribute …
> 
> u = User.new(:born =\> ‘10.10.2007’)  
> puts(u.born) #results in nil
> 
> so now i cannot really validate dates because the string is not stored  
> in the attribute … whereas all other attributes store everything …  
> when i excpect a number in an attribute i can assign a string but the  
> object will stay invalid (if i have a validator)
> 
> so whats the best thing to deal with dates in connection with a web  
> form?

I don’t think the dot notation works for dates. Hyphens and slashes are  
okay though. In terms of validation, a simple trick I use is…

class User

validate :born\_should\_be\_valid\_date

protected

```
def born_should_be_valid_date
  born.to_date
rescue
  errors.add(:born, 'Invalid date for born')
end

```

end

The #to\_date method is provided by Rails and will turn a string, Time or  
Date object into a Date object if it can, and will raise an exception if  
it can’t.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [December 20, 2007, 2:20pm UTC](https://www.ruby-forum.com/t/how-do-you-validate-dates-of-models/123669/4 "2007-12-20T14:20:58Z")

</div>

daniel i dont fully understand your approach … i still cannot assign

u = User.new(:born =\> ‘foo’)

here born will hold nil because it cannot be parsed to a date. but  
normally it should keep the invalid value and indicate that the instance  
is invalid …so the whole to\_date stuff is not needed because i can  
only assign a valid date anyway … if its not valid it sets the  
attribute to nil

the thing is that i want to keep the value and show the user that the  
value is invalid … like with all other attributes

hope you understand what i mean

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [December 20, 2007, 6:33pm UTC](https://www.ruby-forum.com/t/how-do-you-validate-dates-of-models/123669/5 "2007-12-20T18:33:35Z")

</div>

On 12/20/07, Michal G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> the thing is that i want to keep the value and show the user that the  
> value is invalid … like with all other attributes
> 
> hope you understand what i mean

Mass assignment, methods like new and create (i.e. those which take a  
hash of attribute names to variables appear to all nil out values  
that dont match.

But if you instead did.

u = User.new  
u.born = “foo” # or equivalently u[:born] = “foo”

you can get the string back with

u.born\_before\_type\_cast

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
