Blank fields getting validated


#1

Hi,

I am not sure if this is already answered in this list. I couldn’t
find an answer. So I am posting the question.

I have multiple validations for a field. For example:

validates_presence_of :contact_email

validates_format_of :contact_email,
:with =>
/^([^@\s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i,
:message => “is not a valid email address”

When I submit the form with a blank contact_email I get 2 error
messages:
Contact email is not a valid email address
Contact email can’t be blank

If the field is blank the second validations obviously fails.

I think Rails should look for blankness of the field and then just
show one error message saying it’s a blank field.

Any solutions or workarouns?

Thanks much.


#2

Love AJAX wrote:

When I submit the form with a blank contact_email I get 2 error messages:
Contact email is not a valid email address
Contact email can’t be blank

:allow_nil => true

See the Default_Validation_Options under
ActiveRecord::Validations::ClassMethods at http://api.rubyonrails.org/

hth,
Bill


#3

Even this doesn’t work. I checked the values of attributes and blank
fields are not nil. It appears to be blank. So the validations are
taking place.