Custom Validation Messages plugin

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_validation_messages/

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

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

Nice plugin!

Rob

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 :slight_smile:

On 11/26/05, Robert [email protected] 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 :slight_smile:

Nice plugin!

Very nice!

Rob


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Best Regards,
-Larry
“Work, work, work…there is no satisfactory alternative.”
— E.Taft Benson

Well done Hammed!

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

H.

to install the plugin:

 script/plugin install

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

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

Hammed

Excellent, I’m sure this will be a boon for localization efforts too.

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
([email protected])

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

Whow great thanks!

these 2 plugins were exactly what i was looking for !

thanks a bunch

Hello Hammed,

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

Peak Obsession

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
[email protected]