[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Why is ruby ignoring allow_nil?

(D. Alvarado)

2/1/2008 4:26:00 PM

Hi,

In my user model, I'm want to validate a field only if the field is
not empty. So I'm trying

validates_format_of :work_phone, :with => %r{^(\+\d)*\s*(\(\d{3}\)\s*)*
\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$}, :allow_nil => true

but if my field, work_phone, is empty, I get the error that the format
is not valid. What is wrong with the above? - Dave
1 Answer

Eivind Eklund

2/1/2008 4:57:00 PM

0

On Feb 1, 2008 5:29 PM, laredotornado <laredotornado@zipmail.com> wrote:
> Hi,
>
> In my user model, I'm want to validate a field only if the field is
> not empty. So I'm trying
>
> validates_format_of :work_phone, :with => %r{^(\+\d)*\s*(\(\d{3}\)\s*)*
> \d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$}, :allow_nil => true
>
> but if my field, work_phone, is empty, I get the error that the format
> is not valid. What is wrong with the above?

That's a Rails, not Ruby issue. While you may have some luck here,
you're more likely to have luck on the Rails mailing lists (see
http://lists.rubyonrails.org/mailman/list...).

Attempting to help you, I might *guess* that the problem is that you
have an empty field (field = ""), not a nil field (field == nil), but
that is a guess - I don't know Rails and how it handles these things.

Eivind.