Forum: Ruby on Rails Blank fields getting validated

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C0dce094a2af043d002c0c957530003c?d=identicon&s=25 Love AJAX (Guest)
on 2007-03-09 14:45
(Received via mailing list)
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.
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2007-03-09 15:07
(Received via mailing list)
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
C0dce094a2af043d002c0c957530003c?d=identicon&s=25 Love AJAX (Guest)
on 2007-03-10 08:07
(Received via mailing list)
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.
This topic is locked and can not be replied to.