Setting up a Proc for conditional validation (:if option)

I want to use the :if option on one of my validation calls.

Basically I want to validate for a field’s format, but only if the field
is submitted. It’s an optional field.

validates_format_of :EMAIL, :if => Proc.new { |email| ! email.nil? },
:message => ‘is not a valid email address’,
:with => /^[-\w.][email protected]([-\w]+.)+[-\w]{2,4}$/

Is this correct? It doesn’t seem to be working.

Is it easier to use the method or string forms of :if?

Thanks,
Wes

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