Customizing validates_xxx messages


#1

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.

Jake


#2

I just wrote up a hot to page on the Rails wiki

http://wiki.rubyonrails.org/rails/pages/HowToWritePluginToModifyRailsCore

This might be one way to go for you.

-Peter


#3

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.


#4

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
validation.

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.

Jake

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.


#5

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


#6

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 = “

ERROR


html += “

    object.errors.each do | field , message |
    html += “
  • #{message}

  • end
    html += “

html
end

Use in your views as <%= my_error_message(@customer) %>


#7

Hendie,

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.

Hammed


#8

On 11/25/05, Hendie Dijkman wrote:

Dave T. said Ruby and Rails is terribly intuitive!

Terribly intuitive rather than fantastically intuitive? :S gulp