Forum: Ruby on Rails Custom Validation Messages plugin

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.
0bcc972918891ef358b0b3dfb4e7f18b?d=identicon&s=25 hammed (Guest)
on 2005-11-26 09:11
(Received via mailing list)
Yaay! My first plugin.

Validation messages prefixed with the field name have always bothered
me and it seems like I'm not alone. This seemed like a fairly easy fix
to attempt for my first plugin so here it is:

Using this plugin you can specify exactly where in the message you'd
like to see the field name or whether it should be displayed at all.

Simply add a %s to the validation message and you're set.

Guard the message with [ ] brackets and the field name will be
banished from your message for ever.

You may download the plugin from

http://estound.com/svn/rails/plugins/custom_valida...

Enjoy your customized messages.

>From the readme:
===============

This Plugin enables customization of validation error messages.

Default validation error messages are always prefixed with the
offending field name. This plugin allows the developer to specify
exactly where the field name should be displayed in the message or
whether it should be displayed at all.

Tested with Rails 0.14.3

USAGE:

* Include %s in message to specify where the field name should be
displayed:

validates_presence_of :rope, :on => :create, :message => "Can't skip %s"

results in: Can't skip Rope



* Messages may combine %d and %s:

validates_length_of :rope, :minimum=>10, :message => "Need at least %d
feet of %s to hang yourself"

results in: Need at least 10 feet of Rope to hang yourself



* Wrap message in [ ] to avoid field name substition

validates_presence_of :joy, :on => :create, :message => "[Cheer up!]"

results in: Cheer up!



* %s may be specified more than once:

validates_presence_of :joy, :on => :create, :message => "Happy, happy,
%s, %s!"

results in: Happy, happy, Joy, Joy!



* Default behaviour is preserved:

validates_presence_of :joy, :on => :create, :message => "is required"

results in: Joy is required
C618e1d9ffa1bd1a6b6edc8ad4edec63?d=identicon&s=25 mannl (Guest)
on 2005-11-26 11:32
(Received via mailing list)
> results in: Need at least 10 feet of Rope to hang yourself
How morbid..

Nice plugin!



Rob
0bcc972918891ef358b0b3dfb4e7f18b?d=identicon&s=25 hammed (Guest)
on 2005-11-26 15:33
(Received via mailing list)
Thanks.

> > results in: Need at least 10 feet of Rope to hang yourself
> How morbid..

I tried to make the examples following make up for it :)
Ddc850a63a423c3ff963bdf3a097652f?d=identicon&s=25 ldk2005 (Guest)
on 2005-11-26 16:42
(Received via mailing list)
On 11/26/05, Robert <mannl@gmx.com> wrote:
>
> > results in: Need at least 10 feet of Rope to hang yourself
> How morbid..


Yup! But, it's sure hard to miss the  point :)


Nice plugin!


 Very nice!

Rob
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>



--
Best Regards,
-Larry
"Work, work, work...there is no satisfactory alternative."
            --- E.Taft Benson
0091f92762685860109bbcb02edfdf27?d=identicon&s=25 arav2132 (Guest)
on 2005-11-26 19:35
(Received via mailing list)
to install the plugin:

     script/plugin install
http://estound.com/svn/rails/plugins/custom_valida...
2b7d58ac3b63af8e35d63e581a72d9c4?d=identicon&s=25 hendie (Guest)
on 2005-11-26 21:36
(Received via mailing list)
Well done Hammed!

Just what I needed! It works as advertised. :)

H.
4bd34a2216dc8bdbf1f017f64e4d59e8?d=identicon&s=25 kyle (Guest)
on 2005-11-26 21:48
(Received via mailing list)
Excellent, I'm sure this will be a boon for localization efforts too.
0bcc972918891ef358b0b3dfb4e7f18b?d=identicon&s=25 hammed (Guest)
on 2005-11-27 01:22
(Received via mailing list)
Thanks to all for the positive feedback. If no issues are found with
this, I'll submit it as a patch.

Hammed
Ed494974e25cc149ea4aba419e351e06?d=identicon&s=25 drawn.boy (Guest)
on 2005-11-28 01:29
(Received via mailing list)
Hi. Hammed, it's a nice plugin!

> Thanks to all for the positive feedback.
> If no issues are found with this, I'll submit it as a patch.

Just now my group is creating a similar plugin, and
it has some function not included in your plugin.

If you find the function useful, could you merge it to your
code before submitting the patch?
(I think marge these, it's more accetable than submit individually...)

The new function customizes field names in the error view of validate.
Originaly this function is for localization to Japanese environments,
but I think it is also useful for in non-Japanese environments.

Example usage:

At a model file

set_field_names :title => 'Entry Name', :description => 'Entry
Description'

This changes the names of the error fields "title" and "description"
to "Entry Name" and "Entry Description", respectively.
And it also can change 2byte-charecter (not support DB server's field
name).

The plugin files are stored under
http://nowherenear.net:3333/tmp/set_field_names/

I hope you like it!

Sorry for my broken English...

--
hideaki
(drawn.boy@gmail.com)
0bcc972918891ef358b0b3dfb4e7f18b?d=identicon&s=25 hammed (Guest)
on 2005-11-28 05:26
(Received via mailing list)
Hello Hideaki,

I think you should add your plugin to the list here:

http://wiki.rubyonrails.com/rails/pages/Plugins

You may also want to announce it on the list to expose it to a larger
audience than this thread.

The ability to specify more meaningful display names is definitely
useful. I've dropped it into my plugins folder.

Regarding merging the two features, I think they both have a better
chance of being accepted if they're submitted separately since they
address related but slightly different requirements.

cheers!

Hammed
Ed494974e25cc149ea4aba419e351e06?d=identicon&s=25 drawn.boy (Guest)
on 2005-11-28 10:13
(Received via mailing list)
Hello Hammed,

> I think you should add your plugin to the list here:
>
> http://wiki.rubyonrails.com/rails/pages/Plugins
>
> You may also want to announce it on the list to expose it to a larger
> audience than this thread.

There is also some truth in your perspective.
So, I'll try to add plugin page, and hear everyone's voice.

Thank You!

--
hideaki
drawn.boy@gmail.com
8490003c67ae6379c01fb808e1eb1aaf?d=identicon&s=25 Jonas Wouters (zertox)
on 2005-12-07 23:17
Whow great thanks!

these 2 plugins were exactly what i was looking for !

thanks a bunch
This topic is locked and can not be replied to.