Forum: Ruby on Rails Custom validation error messages.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Df679bd3d287a21d5ed6020bae384e17?d=identicon&s=25 Diego (Guest)
on 2007-05-09 03:46
(Received via mailing list)
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.

Cheers,
Diego
F3af88ca25a1607014c3c11fed942bd7?d=identicon&s=25 Mark Bates (markbates)
on 2007-05-09 04:41
Hey Diego, I believe you're talking about the plugin I wrote,
http://rubyforge.org/projects/railserrorsfor/, 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 topic is locked and can not be replied to.