Forum: Ruby on Rails Customizing validates_xxx 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.
Jake J. (Guest)
on 2005-11-26 01:26
Hello group-

The validates_xxx helpers provide a ':message' parameter to customize
the message somewhat.  However, they always prefix this message with the
table column name that caused the trouble.  Is there a way to turn this
part off so that the custom message is the only thing displayed?

i.e., rather than say:

   Name_funky is already being used.

I'd like a validated_uniqueness_of to say something like:

   Your funky name is already being used.  Please choose another.

petermichaux (Guest)
on 2005-11-26 04:44
(Received via mailing list)
I just wrote up a hot to page on the Rails wiki

This might be one way to go for you.

petermichaux (Guest)
on 2005-11-26 04:48
(Received via mailing list)
My example is not exactly what you want to do but using a plugin to
the core to stop including the column name could be done I think.
Jake J. <> <jakejanove (Guest)
on 2005-11-26 05:28
Thanks.  I've seen how to change the default error messages and I think
that's useful in many cases.  However, I'm curious how 'real' apps do

The validates_xxx framework seems pretty limited -- not quite as limited
as the scaffolding, but in a similar vein.  I imagine commercial apps
want a more custom way of handling validation results, but I don't quite
see the best way to do it.

What I mean to say is that the helpers are nice and suit many needs, but
do people generally throw them out and replace them with custom
validation stuff?

In the end, the helpers are useful and I don't want to rewrite all that
logic, but I don't quite see the most economical way of attaching my own
view to these helpers.


petermichaux wrote:
> My example is not exactly what you want to do but using a plugin to
> modify
> the core to stop including the column name could be done I think.
Hendie D. (Guest)
on 2005-11-26 08:10
(Received via mailing list)
At the risk of being flamed, "Me too! Me too!"

I had expected (Dave T. said Ruby and Rails is terribly
intuitive!) that if I override the error message with :message, that
it would replace the whole thing, as Jake also lamented here. Like him
(and his "Name funky" attribute) I have an attribute that doesn't work
very well in a message, and of course it blows the whole i18n
possibility right out the water!

Hendie Dijkman
Calgary, AB
Daniel W. (Guest)
on 2005-11-26 08:30
It is not that hard, i use the following method :

validates_presence_of :email , :message=>'Your message'

then you create a method in you helper that you pass a object or add a
method to the rails code

def my_error_message(object)

html = "<h1>ERROR<h1>"
html += "<ul>"
object.errors.each do | field , message |
  html += "<li>#{message}</li>"
html += "</ul>"

Use in your views as <%= my_error_message(@customer) %>
petermichaux (Guest)
on 2005-11-26 08:46
(Received via mailing list)
On 11/25/05, Hendie Dijkman wrote:
> Dave T. said Ruby and Rails is terribly intuitive!

Terribly intuitive rather than fantastically intuitive?   :S gulp
hammed (Guest)
on 2005-11-26 10:11
(Received via mailing list)

I just posted a message announcing a plugin which allows you to
customize validation messages.

A side effect of this plugin is that it allows for easier localization
of messages since you're free to put the fieldname placeholder
anywhere you like.

Let me know if it works for you.

This topic is locked and can not be replied to.