Forum: Ruby on Rails Change default validation error messages.

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
05bf52a3e873f6847b05536a99bec4ad?d=identicon&s=25 Diego (Guest)
on 2007-02-27 06:39
(Received via mailing list)
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.

29a1b90442043a2b9f4d5859df00dc98?d=identicon&s=25 DKG (Guest)
on 2007-02-27 06:56
(Received via mailing list)
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 and look at
the CVS, you can see it done there.

40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (Guest)
on 2007-02-27 07:31
(Received via mailing list)
Hey Diego, you can simply add the following to your validation:

:message => "My validation message."


validates_presence_of :name, :message => "Name field cannot be blank."

Good luck,

70225136eacd3d870f64e03bff678655?d=identicon&s=25 Russell Norris (Guest)
on 2007-02-27 15:28
(Received via mailing list)
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
attribute already as errors#each_full [which is what normally gets
IIRC]. Even if you write your own error message handling you're better
not adding the attribute name and just using errors#each_full. YMMV

05bf52a3e873f6847b05536a99bec4ad?d=identicon&s=25 Diego (Guest)
on 2007-02-28 00:52
(Received via mailing list)
Thanks Russell and all others for taking the time to reply. I will
give these a try.
This topic is locked and can not be replied to.