# Customizing validates\_xxx messages

**URL:** <https://www.ruby-forum.com/t/customizing-validates-xxx-messages/48872>\
**Category:** Rails\
**Created:** [November 26, 2005, 12:26am UTC](https://www.ruby-forum.com/t/customizing-validates-xxx-messages/48872 "2005-11-26T00:26:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [November 26, 2005, 12:26am UTC](https://www.ruby-forum.com/t/customizing-validates-xxx-messages/48872/1 "2005-11-26T00:26:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [November 26, 2005, 3:44am UTC](https://www.ruby-forum.com/t/customizing-validates-xxx-messages/48872/2 "2005-11-26T03:44:53Z")

</div>

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

[http://wiki.rubyonrails.org/rails/pages/HowToWritePluginToModifyRailsCore](http://wiki.rubyonrails.org/rails/pages/HowToWritePluginToModifyRailsCore)

This might be one way to go for you.

-Peter

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [November 26, 2005, 3:48am UTC](https://www.ruby-forum.com/t/customizing-validates-xxx-messages/48872/3 "2005-11-26T03:48:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [November 26, 2005, 4:28am UTC](https://www.ruby-forum.com/t/customizing-validates-xxx-messages/48872/4 "2005-11-26T04:28:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [November 26, 2005, 7:10am UTC](https://www.ruby-forum.com/t/customizing-validates-xxx-messages/48872/5 "2005-11-26T07:10:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [November 26, 2005, 7:30am UTC](https://www.ruby-forum.com/t/customizing-validates-xxx-messages/48872/6 "2005-11-26T07:30:26Z")

</div>

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) %\>

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [November 26, 2005, 9:11am UTC](https://www.ruby-forum.com/t/customizing-validates-xxx-messages/48872/7 "2005-11-26T09:11:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [November 26, 2005, 7:46am UTC](https://www.ruby-forum.com/t/customizing-validates-xxx-messages/48872/8 "2005-11-26T07:46:35Z")

</div>

On 11/25/05, Hendie Dijkman wrote:

> Dave T. said Ruby and Rails is terribly intuitive!

Terribly intuitive rather than fantastically intuitive? :S gulp
