Custom validation error messages

Hi all,

I have a few questions regarding the error messages which are produced
by default. For example, if I have “validates_uniqueness_of” I may get
an error message "Surname has already been taken’

What if I want a custom message without prefixing the attribute name?
I’ve come across a plugin which allows me to do this:

validates_uniqueness_of :name, :message => ‘^The surname supplied is
not valid.’

Whereby the use of the ^ says that error message should be output as
is, without any prefixing, but just without the ^.

Are there any other cleaner alternatives than this plugin? What if
(and most likely it may) the Rails full_messages changes and things
then break? BTW full_messages is overriden by said plugin. I know it’s
probably being picky of me, and the plugin is almost there and a clean
enough solution.

Thought I’d ask.


Hey Diego, I believe you’re talking about the plugin I wrote,, which, by the way, is now
a gem, and I would definitely recommend using that.

I don’t override any Rails methods, exept for error_messages_for, so if
you’re worried about how things like full_messages work, don’t worry,
they’re safe.

I use this gem in all my Rails projects, so you can rest assured that
I’m constantly keeping it up-to-date, and as bug free as a I can,
because I need it.

All you need to do is install the gem, and require it, and you’re done.
There are other great parts to this gem as well, like the ability to
have multiple object errors printed out in the same errors ‘box’, as
well as the ability to specify the partial you would like to use to
format the errors.

Let me know if you find any bugs, or have any features you would like to
add, I’m all ears!

Good luck!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs