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,
On Feb 12, 2:09 pm, “J. mp” [email protected] 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.
Alex W. wrote:
Ed Hickey wrote:
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
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?
J. mp wrote:
Alex W. wrote:
Ed Hickey wrote:
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
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
If it lacks documentation, I wouldn’t mind to write a bit about it, but
I need a starting point.
Thanks a lot all for the help, it is very useful
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)
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 [email protected] wrote:
Posted via http://www.ruby-forum.com/.
–