# Only one error message for a field

**URL:** <https://www.ruby-forum.com/t/only-one-error-message-for-a-field/153721>\
**Category:** Rails\
**Created:** [November 20, 2008, 1:42pm UTC](https://www.ruby-forum.com/t/only-one-error-message-for-a-field/153721 "2008-11-20T13:42:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![thaniyarasu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@thaniyarasu](https://www.ruby-forum.com/u/thaniyarasu)\
**Post date:** [November 20, 2008, 1:42pm UTC](https://www.ruby-forum.com/t/only-one-error-message-for-a-field/153721/1 "2008-11-20T13:42:45Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![thaniyarasu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@thaniyarasu](https://www.ruby-forum.com/u/thaniyarasu)\
**Post date:** [November 20, 2008, 3:53pm UTC](https://www.ruby-forum.com/t/only-one-error-message-for-a-field/153721/2 "2008-11-20T15:53:23Z")

</div>

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/Validations/ClassMethods.html#M001639](http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001639)

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 Z.  
Zapient, LLC  
Ruby on Rails Development and Consulting

[http://www.zapient.com](http://www.zapient.com)  
[http://www.fromjavatoruby.com](http://www.fromjavatoruby.com)

On Nov 20, 4:42 am, Thani A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
