Rob Biedenharn wrote: > On Jan 12, 2010, at 4:51 PM, Danimal wrote: > >> # 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.
Please don't use a more sophisticated expression. The only such *correct* regexps I'm aware of for e-mail addresses are on the order of a page in length. An *incorrect* regexp will reject valid e-mail addresses (this actually happens surprisingly frequently). Just check for the @ and . and trust the user for the rest. > 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. In the latter case, it's easy to write custom validators. > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > [email protected] Best, -- Marnen Laibow-Koser http://www.marnen.org [email protected] -- Posted via http://www.ruby-forum.com/.
-- 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.

