Forum: Ruby on Rails only one error message for a field

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.
C5c0e0499446b84fd3bcc61cf7862539?d=identicon&s=25 Thani Ararsu (thaniyarasu)
on 2008-11-20 13:42
in my User model i am using validations like this

class User < ActiveRecord::Base
validates_presence_of :email
validates_format_of   :email ,:with=>/something/

end


if my email field is blank then i am getting 2 error messages
but i want only one that should be first one (here it is presence)

any idea?
F47bebf5f4b10b709a59310027c8f977?d=identicon&s=25 Robert Zotter (Guest)
on 2008-11-20 15:53
(Received via mailing list)
Thani,

You can use :allow_nil, or :allow_blank to skip validation.

validates_format_of :email, :with => /whatever/, :allow_blank => true

# :allow_nil - If set to true, skips this validation if the attribute
is nil (default is false).
# :allow_blank - If set to true, skips this validation if the
attribute is blank (default is false).
http://api.rubyonrails.org/classes/ActiveRecord/Va...

You will probably want to use allow_blank because if you create an
email via params it could come back as an empty string.

--
Robert Zotter
Zapient, LLC
Ruby on Rails Development and Consulting

http://www.zapient.com
http://www.fromjavatoruby.com

On Nov 20, 4:42 am, Thani Ararsu <rails-mailing-l...@andreas-s.net>
This topic is locked and can not be replied to.