On Jan 12, 2010, at 4:51 PM, Danimal wrote:

Hello!

Has anyone come across a nice, elegant way to handle multiple
validation failures on a single field? For example... in the typical
login, you might have an email field that has three validations: not
blank, less than a max length and matches a format regex. But if I
submit the login/signup form with a blank I get back:

# Email address is too short (minimum is 6 characters)
# Email address should look like an email address.
# Email address can't be blank

I'd rather be able to specify (cleanly and easily) on a model some
kind of order of importance and then only show the first validation
failure for that field.

Thoughts? Advice?

TIA!

-Danimal

These all seem to be aspects of the same thing. If an email looks like an email address, then it is presumably at least 6 characters ([email protected] ) and thus non-blank.

validates_format_of :email_address,
      :with => /\A([...@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i,
      :message => 'should look like an email address'

or even some more sophisticated expression. You could toss in :allow_nil=>false or :allow_blank=>false even though the default for both is already false.

You could write your own validate() method and take over all the validation, but that might not be a viable option if there are several fields with validations.

-Rob

Rob Biedenharn          http://agileconsultingllc.com
[email protected]



-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.


Reply via email to