# Standard ActiveRecord validation message

**URL:** <https://www.ruby-forum.com/t/standard-activerecord-validation-message/171041>\
**Category:** Rails\
**Created:** [June 25, 2009, 5:36pm UTC](https://www.ruby-forum.com/t/standard-activerecord-validation-message/171041 "2009-06-25T17:36:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [June 25, 2009, 5:36pm UTC](https://www.ruby-forum.com/t/standard-activerecord-validation-message/171041/1 "2009-06-25T17:36:50Z")

</div>

in testing my app I just use the validations

validates\_presence\_of :first\_name, :last\_name, :email  
validates\_presence\_of :academy, :message =\> “You must select an  
Academy”  
validates\_presence\_of :role, :message =\> “You must select a role”

standard error\_messages are fine for most of my fields , resulting  
in :

5 errors prohibited this data from being saved  
There were problems with the following fields:

# Last name can’t be blank

# Email can’t be blank

# First name can’t be blank

but 2 messages are a little bit confusing :

# Role You must select a role

# Academy You must select an Academy

the model names are embedded into the error message… how can I  
suppress them and get only the message…

# You must select a role

# You must select an Academy

thanks fyh

erwin

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [June 25, 2009, 6:19pm UTC](https://www.ruby-forum.com/t/standard-activerecord-validation-message/171041/2 "2009-06-25T18:19:02Z")

</div>

2 options I can think of:

#1 - change the message of each to “must be selected.” It’s not perfect,  
but the intent of the message is clear.

#2 - define your own validations using errors.add\_to\_base. See here for  
an example:

> **[rails.info](http://guides.rails.info/activerecord_validations_callbacks.html#errorsadd-to-base)**
>
> Ruby on Rails,Rails Tutorial,Ruby Basics,Rails 4,Bar Rails,Rail

So, for example, you might try something like:

validate :academy\_is\_selected

def academy\_is\_selected  
errors.add\_to\_base(“You must select an Academy”) if  
self.academy.blank?  
end

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [June 25, 2009, 6:43pm UTC](https://www.ruby-forum.com/t/standard-activerecord-validation-message/171041/3 "2009-06-25T18:43:57Z")

</div>

Yeah, sorry, I meant the model attribute and meant model.

I’m not sure how you would arrange the error messages in a specific  
order like that, but you can place them alongside each field.

> **[rails.info](http://guides.rails.info/activerecord_validations_callbacks.html#customizing-the-error-messages-html)**
>
> Ruby on Rails,Rails Tutorial,Ruby Basics,Rails 4,Bar Rails,Rail

For example, in my app I use:

ActionView::Base.field\_error\_proc = Proc.new do |html\_tag, instance|  
if html\_tag =~ /label for/  
if instance.error\_message.kind\_of?(Array)  
%(#{html\_tag}  
#{instance.error\_message.join(', and ')}.)  
else  
%(#{html\_tag}  
#{instance.error\_message}.)  
end  
else  
%(#{html\_tag})  
end  
end

(I realize that code isn’t very neat - I wrote it a few months ago and  
it’s worked fine, so I haven’t touched it since.)

And then my forms look like:

**\<%= f.label :name %\>**  
 \<%= f.text\_field :name, :size =\> 50 %\>

**\<%= f.label :email %\>**  
 \<%= f.text\_field :email, :size =\> 50 %\>

**\<%= f.label :password %\>**  
 \<%= f.password\_field :password, :size =\> 50 %\>

And in my CSS file: span.validation-error{color:red}

So, whenever the form is submitted and has errors, the labels are  
replaced with the error messages in red.

So this:  
Email  
(email text field)

becomes

Email is too short, and does not look valid  
(email text field)

Anyway, that’s my solution, and I think it looks nice, but of course  
feel free to be creative.

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [June 25, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/standard-activerecord-validation-message/171041/4 "2009-06-25T18:45:04Z")

</div>

Chris H. wrote:

> Yeah, sorry, I meant the model attribute and meant model.

Geez, I am not articulating myself clearly today. I meant the model  
attribute and SAID model.

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [June 25, 2009, 6:28pm UTC](https://www.ruby-forum.com/t/standard-activerecord-validation-message/171041/5 "2009-06-25T18:28:28Z")

</div>

Thanks , I just realized that it was not the Model names, but rather  
the field names in my User model… you’re right that’s fine now…

btw , is there any way to get the error messages in a sequence  
similar to the textfields sequence… ?  
I doubt as it’s a hash and so non-ordered , but …

Erwin

On 25 juin, 18:19, Chris H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
