# Override validates message

**URL:** <https://www.ruby-forum.com/t/override-validates-message/85297>\
**Category:** Rails\
**Created:** [February 12, 2007, 1:09pm UTC](https://www.ruby-forum.com/t/override-validates-message/85297 "2007-02-12T13:09:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [February 12, 2007, 1:09pm UTC](https://www.ruby-forum.com/t/override-validates-message/85297/1 "2007-02-12T13:09:38Z")

</div>

Hi all,  
I have the some fields being validated on my model and if the validation  
fails I can override the Message by using :message but how can I  
override the field name that apears on the messge:  
Intead of password is too long -\> where “is too long” i can set in  
message  
bu I want the message to be  
“field\_1 is too long”

Thnaks,

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [February 12, 2007, 8:05pm UTC](https://www.ruby-forum.com/t/override-validates-message/85297/2 "2007-02-12T20:05:53Z")

</div>

On Feb 12, 2:09 pm, “J. mp” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have the some fields being validated on my model and if the validation  
> fails I can override the Message by using :message but how can I  
> override the field name that apears on the messge:  
> Intead of password is too long → where “is too long” i can set in  
> message  
> bu I want the message to be  
> “field\_1 is too long”

You could specify an arbitrary attribute name to  
ActiveRecord::Errors#add. Arguably, this is an abuse of the API and  
may not work in future releases of ActiveRecord.

Alternatively, use ActiveRecord::Errors#add\_to\_base to add a message  
to the base object instead of a specific attribute. This gives you  
control of the full error message.

Finally, you could use a localization plugin to handle translation of  
error messages in the view.

Ciao,  
Sheldon.

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [February 12, 2007, 8:11pm UTC](https://www.ruby-forum.com/t/override-validates-message/85297/3 "2007-02-12T20:11:53Z")

</div>

[http://www.agilewebdevelopment.com/plugins/custom\_error\_message](http://www.agilewebdevelopment.com/plugins/custom_error_message)

ed

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [February 12, 2007, 8:22pm UTC](https://www.ruby-forum.com/t/override-validates-message/85297/4 "2007-02-12T20:22:34Z")

</div>

Ed Hickey wrote:

> [http://www.agilewebdevelopment.com/plugins/custom\_error\_message](http://www.agilewebdevelopment.com/plugins/custom_error_message)
> 
> ed

I use this and love it.

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [February 13, 2007, 12:55am UTC](https://www.ruby-forum.com/t/override-validates-message/85297/5 "2007-02-13T00:55:50Z")

</div>

Alex W. wrote:

> Ed Hickey wrote:
> 
> > [http://www.agilewebdevelopment.com/plugins/custom\_error\_message](http://www.agilewebdevelopment.com/plugins/custom_error_message)
> > 
> > ed
> 
> I use this and love it.

Could not found any documentation for  
[http://www.agilewebdevelopment.com/plugins/custom\_error\_message](http://www.agilewebdevelopment.com/plugins/custom_error_message)

Regarding the Sheldon 2nd proposal:

> Alternatively, use ActiveRecord::Errors#add\_to\_base to add a message  
> to the base object instead of a specific attribute. This gives you  
> control of the full error message.

Can I do this per attribute, how would I do that? Must I have my own  
validation methods and not rely on Ruby validates\_ methods?

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [February 13, 2007, 8:01pm UTC](https://www.ruby-forum.com/t/override-validates-message/85297/6 "2007-02-13T20:01:09Z")

</div>

J. mp wrote:

> Alex W. wrote:
> 
> > Ed Hickey wrote:
> > 
> > > [http://www.agilewebdevelopment.com/plugins/custom\_error\_message](http://www.agilewebdevelopment.com/plugins/custom_error_message)
> > > 
> > > ed
> > 
> > I use this and love it.
> 
> Could not found any documentation for  
> [http://www.agilewebdevelopment.com/plugins/custom\_error\_message](http://www.agilewebdevelopment.com/plugins/custom_error_message)
> 
> Regarding the Sheldon 2nd proposal:
> 
> > Alternatively, use ActiveRecord::Errors#add\_to\_base to add a message  
> > to the base object instead of a specific attribute. This gives you  
> > control of the full error message.
> 
> Can I do this per attribute, how would I do that? Must I have my own  
> validation methods and not rely on Ruby validates\_ methods?

From the missing answers on this topic I assume that Rails haven’t a  
flexible solution for my problem. I have to rely on plugins or rely on  
my own validations. Am I right?  
Anyway, can someone provide-me docs or sample for  
[http://www.agilewebdevelopment.com/plugins/custom\_error\_message](http://www.agilewebdevelopment.com/plugins/custom_error_message)  
If it lacks documentation, I wouldn’t mind to write a bit about it, but  
I need a starting point.

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [February 14, 2007, 11:20am UTC](https://www.ruby-forum.com/t/override-validates-message/85297/7 "2007-02-14T11:20:30Z")

</div>

Thanks a lot all for the help, it is very useful

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [February 14, 2007, 11:40am UTC](https://www.ruby-forum.com/t/override-validates-message/85297/8 "2007-02-14T11:40:16Z")

</div>

J. mp wrote:

> Thanks a lot all for the help, it is very useful

Just one more question. I have no experience in how to install plugins.  
How can I install this? (sorry for my newbie kink of questions, but is  
becuse I’m getting started with ruby on rails)

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [February 13, 2007, 8:38pm UTC](https://www.ruby-forum.com/t/override-validates-message/85297/9 "2007-02-13T20:38:44Z")

</div>

it doesn’t lack documentation - see the README file inccluded in the  
plugin:

# Custom Error Message

This plugin gives you the option to not have your custom validation  
error  
message  
prefixed with the attribute name. Ordinarily, if you have, say:

```
validates_acceptance_of :accepted_terms, :message => 'Please accept 

```

the  
terms of service’

You’ll get the following error message:

```
Accepted terms Please accept the terms of service

```

This plugin allows you to omit the attribute name for specific messages.  
All  
you have to do  
is begin the message with a ‘^’ character. Example:

```
validates_acceptance_of :accepted_terms, :message => '^Please accept 

```

the  
terms of service’

## Detail

Redefines the ActiveRecord::Errors::full\_messages method:  
Returns all the full error messages in an array. ‘Base’ messages are  
handled as usual.  
Non-base messages are prefixed with the attribute name as usual UNLESS  
they  
begin with ‘^’  
in which case the attribute name is omitted.

On 2/13/07, J. mp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–
