Hi --

On Thu, 30 Mar 2006, Dara Sanderson wrote:

> Hello,
>
> I hope someone can help me or at least confirm that I'm not going
> insane.
>
> I have a model in which I'm trying to validate information before it
> saves.
> One particular field is for and expiration date.  The code in my model
> to do the validation looks like this:
>
> validates_format_of :expiration,
>                     :with       => %r{^[\d]{4,4}$},
>                     :message    => "must be formatted (mmyy)"
>
>
> The problem is if someone enter a date that starts with a zero like
> "0309"  The validation somehow just doesn't "see" the leading zero and
> keeps returning an error as though the user isn't submitting it
> properly.  I did verify that the controller is definetly sending all
> four digits in the post.
>
> I tried different combinations like: r{^(0|1)[\d]{3}$} but no matter
> what I did the zero was ignored.  I finally had to change it to look for
> 3 or 4 digits just to make it work but that of course isn't proper
> validation as my submissions must have 4 digits.
>
> Can anyone help me?  Am I going crazy here?

My guess is that somewhere along the line the month is being turned
into an integer and back to a string, so that "03" becomes 3 and then
"3".  I don't know where, though.


David

-- 
David A. Black (dblack / wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black