Hi,
I have a set of validations like:
validates_format_of :city,
:with => /^[a-zA-Z\s’&]+$/,
:message => “is not a valid name”
validates_format_of :zip,
:with => /^[\d]{5}+$/,
:allow_nil => true,
:message => “is not a valid zip code”
But I don’t have a validates_presence_of :city, :zip.
I want to validate the format only if user has entered some value into
the fields. But both the _format_of hooks are trying to validate an
empty value. Any idea how I can stop this behavior?
Thanks.
Add :allow_nil => true to the city validation. You only have it on the
zip code validation.
-Bill
Love AJAX wrote:
:message => "is not a valid zip code"
–
Sincerely,
William P.
:allow_nil => true didn’t work on these validations. Instead I had to
do something like :if => Proc.new{|profile| profile != “”}. This works
fine. So my code now looks like this:
validates_format_of :zip,
:with => /^[\d]{5}+$/,
:if => Proc.new {|profile| profile.zip != “”},
:message => “is not a valid zip code”
validates_format_of :city,
:with => /^[a-zA-Z\s'&]+$/,
:if => Proc.new {|profile| profile.city != “”},
:message => “is not a valid name”
See the last sentence of
Peak Obsession.
Thanks much.