On Nov 16, 6:22 am, "Todd A. Jacobs" <tjacobs-sndr-
[email protected]> wrote:
> On Mon, Nov 16, 2009 at 01:15:41AM -0800, ralu wrote:
> > you  are giving :maxlength=>5 in form rite? y cant u give the :maximum
> > => 5 in ur model , dat would wrk i think so.
>
> No, it wouldn't. How would that validate that a number is exactly 5
> digits long? If someone puts in only 4, then it's still an error.
>
> The only way to do it currently, as far as I can tell, is with a regex
> like so:
>
>     validates_format_of :zipcode, :with => /\d{5}/
>
> validates_length_of is clearly broken (or improperly documented) in
> 2.3.4. Luckily, there's more than one way to achieve the same validation
> in this particular instance.

Validates_length_of is checking the .size property of the field; if
it's already been converted to an integer, that returns the size (in
bytes) not the number of digits.

The above validates_format_of isn't going to work on a repeat save, as
a zipcode of '01234' will be saved to the DB as 1234, and (unless
you're doing formatting not shown here) cause the validation to fail.

I tend to agree - zip codes aren't really numbers, even if they look
like them. And if you implement them as strings, you won't even break
a sweat when your boss comes in and says, "now we need to support
CANADA!"... :)

--Matt Jones

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to