How do I allow an empty value in my model?

Hi,

If a user enters a value for the field “work_phone”, I want to
validate it against a reg-exp. However, if the field is completely
blank, I’d like to allow that too. This doesn’t work, though

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_empty
=> true

What is the right way to validate the field in the model? - Dave

[email protected] wrote:

Hi,

If a user enters a value for the field “work_phone”, I want to
validate it against a reg-exp. However, if the field is completely
blank, I’d like to allow that too. This doesn’t work, though

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_empty
=> true

What is the right way to validate the field in the model? - Dave

if you are on 2.0.2 then you can use :allow_blank => true

I believe it’s allow_nil => true.

Peace,
Phillip

allow_blank is the answer. 5 stars. -

On Feb 1, 2:24 pm, “Vapor …” [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs