# Change default validation error messages

**URL:** <https://www.ruby-forum.com/t/change-default-validation-error-messages/86962>\
**Category:** Rails\
**Created:** [February 27, 2007, 6:39am UTC](https://www.ruby-forum.com/t/change-default-validation-error-messages/86962 "2007-02-27T06:39:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diego](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Diego](https://www.ruby-forum.com/u/Diego)\
**Post date:** [February 27, 2007, 6:39am UTC](https://www.ruby-forum.com/t/change-default-validation-error-messages/86962/1 "2007-02-27T06:39:59Z")

</div>

Hi all,

I was wondering if it was possible to globally change the Rails  
validation error messages?

For example, when I enter a blank field I get the message:

“Caption can’t be blank”

I’d like for blank fields to show

“Caption is a mandatory field”

I know I can give each validation a custom error message, but I’d like  
to change it globally.

Cheers,  
Diego

---

<div class="post-metadata">

**Author:** ![Diego](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Diego](https://www.ruby-forum.com/u/Diego)\
**Post date:** [February 27, 2007, 6:56am UTC](https://www.ruby-forum.com/t/change-default-validation-error-messages/86962/2 "2007-02-27T06:56:00Z")

</div>

I got a hack. Somewhere in enviornment.rb set a bunch of global  
variables with your custom error messages. Then in each model, set the  
error message to the global variable.

If you go to [http://dev.subimage.com/projects/substruct](http://dev.subimage.com/projects/substruct) and look at  
the CVS, you can see it done there.

David

---

<div class="post-metadata">

**Author:** ![Diego](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Diego](https://www.ruby-forum.com/u/Diego)\
**Post date:** [February 27, 2007, 7:31am UTC](https://www.ruby-forum.com/t/change-default-validation-error-messages/86962/3 "2007-02-27T07:31:02Z")

</div>

Hey Diego, you can simply add the following to your validation:

:message =\> “My validation message.”

e.g.

validates\_presence\_of :name, :message =\> “Name field cannot be blank.”

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![Diego](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Diego](https://www.ruby-forum.com/u/Diego)\
**Post date:** [February 27, 2007, 3:28pm UTC](https://www.ruby-forum.com/t/change-default-validation-error-messages/86962/4 "2007-02-27T15:28:59Z")

</div>

validates\_presence\_of :caption, :message =\> “is a mandatory field”

Don’t add the field name as part of the message because it gets added to  
the  
attribute already as errors#each\_full [which is what normally gets  
called  
IIRC]. Even if you write your own error message handling you’re better  
off  
not adding the attribute name and just using errors#each\_full. YMMV

RSL

---

<div class="post-metadata">

**Author:** ![Diego](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Diego](https://www.ruby-forum.com/u/Diego)\
**Post date:** [February 28, 2007, 12:52am UTC](https://www.ruby-forum.com/t/change-default-validation-error-messages/86962/5 "2007-02-28T00:52:55Z")

</div>

Thanks Russell and all others for taking the time to reply. I will  
give these a try.
