# Weird validates\_format\_of problem

**URL:** <https://www.ruby-forum.com/t/weird-validates-format-of-problem/111749>\
**Category:** Rails\
**Created:** [August 10, 2007, 1:32pm UTC](https://www.ruby-forum.com/t/weird-validates-format-of-problem/111749 "2007-08-10T13:32:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobycatlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tobycatlin](https://www.ruby-forum.com/u/tobycatlin)\
**Post date:** [August 10, 2007, 1:32pm UTC](https://www.ruby-forum.com/t/weird-validates-format-of-problem/111749/1 "2007-08-10T13:32:25Z")

</div>

Hi,

I have an odd problem when trying to match a hyphen in a  
validates\_format\_of validation:

class DataDate \< ActiveRecord::Base

validates\_format\_of :date, :with =\> /-/

end

If I try to create a new instance either in my test method or in  
script/console:

> > test = DataDate.new(:date =\> ‘2007-08-10’)  
> > =\> #\<DataDate id: nil, date: nil, created\_at: nil, updated\_at: nil\>
> 
> > test.valid?  
> > =\> false

I get a ‘is invalid’ error on the date.

Does anyone have any idea why this might be.  
script/console and my unit test report $KCODE to be UTF8 - I thought  
it might be character encoding but apparently not. I’ve set the  
character encoding in my terminal, the class and unit test files and  
when using script/console to be UTF8/NONE but neither makes any  
difference.

I’m on a Mac, using TextMate which saves the files in UTF8, using edge  
rails revision 7277, mysql5.

Any help at all would be really appreciated as I have no idea what  
else to do!

Thanks in advance.  
Toby

---

<div class="post-metadata">

**Author:** ![tobycatlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tobycatlin](https://www.ruby-forum.com/u/tobycatlin)\
**Post date:** [August 10, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/weird-validates-format-of-problem/111749/2 "2007-08-10T18:06:05Z")

</div>

Toby C. wrote:

> =\> #\<DataDate id: nil, date: nil, created\_at: nil, updated\_at: nil\>
> 
> > > test.valid?  
> > > =\> false

Is date a database attribute? If so, of what type?

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

---

<div class="post-metadata">

**Author:** ![tobycatlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tobycatlin](https://www.ruby-forum.com/u/tobycatlin)\
**Post date:** [August 10, 2007, 6:27pm UTC](https://www.ruby-forum.com/t/weird-validates-format-of-problem/111749/3 "2007-08-10T18:27:07Z")

</div>

Well actually I simplified my example here but the database type in  
the migration is datetime and I was actually using a correctly  
formatted date of ‘2007-01-01 00:00:00’ in my tests.

---

<div class="post-metadata">

**Author:** ![tobycatlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tobycatlin](https://www.ruby-forum.com/u/tobycatlin)\
**Post date:** [August 10, 2007, 7:02pm UTC](https://www.ruby-forum.com/t/weird-validates-format-of-problem/111749/4 "2007-08-10T19:02:09Z")

</div>

Toby C. wrote:

> Well actually I simplified my example here but the database type in  
> the migration is datetime and I was actually using a correctly  
> formatted date of ‘2007-01-01 00:00:00’ in my tests.

Then you want:

validates\_format\_of :date\_before\_type\_cast, :with =\> /-/

Also ensure date is an accessible (hash settable) attribute.

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

---

<div class="post-metadata">

**Author:** ![tobycatlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tobycatlin](https://www.ruby-forum.com/u/tobycatlin)\
**Post date:** [August 13, 2007, 10:22am UTC](https://www.ruby-forum.com/t/weird-validates-format-of-problem/111749/5 "2007-08-13T10:22:20Z")

</div>

Oh ok - so the string is being typecast to a datetime and so the  
regular expression fails?
