How to change the error message easy way

validates_presence_of :fname

results in the error message

“Fname can’t be blank”.

What I want is “First Name can’t be blank”.

I could do this

def validate
errors.add_to_base(“First Name can’t be blank”) if fname.blank?
end

I find this clunky and I have to put everyrhing in the validate method.
Is
there an easy to get what I want.

I tried this also validates_presence_of :fname , :message => ‘new
message’
still retains the fname.

Assuming that you are using error_messages_for, why don’t you try
overloading it to work the way you want. I would think that’s the
easiest thing to do.

-carl

I could try overloadin it but that causes the detachment issue.

For example if I have column name “fname” and I want the message to
appear
as 'First name" then in the “error_messages_for” method I need to put
the
code to replace 'fname" by “First name”.

Ideally I would like somethign like this.

validates_presence_of :fname, :show_attribute_name => ‘First name’.

Already we have a feature like validates_presence_of :fname , :message
=>
‘new message’ . I guess I need to learn more ruby (& rails) in order to
do
this kind of hack. If someone already knows how to accomplish it easily
please let me know.

-=-

On 8/8/06, Neeraj K. [email protected] wrote:

def validate
errors.add_to_base (“First Name can’t be blank”) if fname.blank?
end

I find this clunky and I have to put everyrhing in the validate method. Is
there an easy to get what I want.

I tried this also validates_presence_of :fname , :message => ‘new message’
still retains the fname.

I’ve always wondered this too. :frowning:

I use the following to give me more flexibility with error messages:

http://rubyforge.org/projects/custom-err-msg/

works perfectly

Mike

Hi,

How about Ruby-GetText-Package?

It’s an library for internationalization.
But I think it’s fit for your purpose.

http://www.yotabanana.com/hiki/?ruby-gettext-howto-rails

On Tue, 15 Aug 2006 10:47:48 -0400

I use the plugin customm-err-msg.
It allows you to omit the attribute name, but then you have to provide
the whole message like

validates_presence_of :fname,
:message => “^ First Name can’t be blank”

Dirk

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