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.
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 and look at
the CVS, you can see it done there.
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.”
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
Thanks Russell and all others for taking the time to reply. I will
give these a try.