Forum: Ruby on Rails Field names in validators

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.
Fdefa9c08c038e0e20a0c45c10a1e678?d=identicon&s=25 Vlad Berditchevskiy (Guest)
on 2006-06-08 02:32
(Received via mailing list)
Hi!

Is there a (simple) way to use alternative field names in validation
messages? For example, if I have a validator like this one:

validates_presence_of :email

how can I make it print a message like "E-Mail Address can't be blank"
instead of "Email can't be blank"?  I can override the "can't be blank"
part by using the :message parameter, but not the "Email" part.


--
\  /                                       vlad@hashbang.de
 \/lad                                     http://www.hashbang.de
Ed5307c79ba5b415b1ddfee48319099b?d=identicon&s=25 Ioana Kanda (Guest)
on 2006-06-08 11:38
(Received via mailing list)
U can override *error_messages_for. *That's the best way to print
specific
error messages.
Or: validates_presence_of :email, :message => "Address can't be blank"
:)
Fdefa9c08c038e0e20a0c45c10a1e678?d=identicon&s=25 Vlad Berditchevskiy (Guest)
on 2006-06-08 19:16
(Received via mailing list)
"Ioana Kanda" <kaio4000@gmail.com> writes:

> U can override *error_messages_for. *That's the best way to print specific
> error messages.

Thank you for pointing that out! I looked at the implementation of this
method and now I see that it's actually
ActiveRecotd::Errors::full_messages,
which composes a line. Then it calls human_attribute_name, which, for
some reason, is not in the documentation.

I'm wondering, why there is no standard way to supply *really*
human readable names? There are plenty of cases, where simple
capitalization of database field won't do it:

- Internationalization
- Customization
- Legacy schemas
- Unusual capitalization or special characters
- Sometimes you may just want a better looking name

> Or: validates_presence_of :email, :message => "Address can't be blank" :)

Hehe, it won't print the dash between "E" and "mail". ;-)


--
\  /                                       vlad@hashbang.de
 \/lad                                     http://www.hashbang.de
Ed5307c79ba5b415b1ddfee48319099b?d=identicon&s=25 Ioana Kanda (Guest)
on 2006-06-08 22:01
(Received via mailing list)
In the api there is no presence of <human_attribute_name>, aparently,
but I
google it.

def human_attribute_name (attr)
    return case attr
           when 'email' then 'E-mail:D'
           else super.human_attribute_name attr
           end
end

But look what I found:
http://wiki.rubyonrails.org/rails/pages/HowToUseVa...
(good article for me to read it too). Heh
Fdefa9c08c038e0e20a0c45c10a1e678?d=identicon&s=25 Vlad Berditchevskiy (Guest)
on 2006-06-11 01:38
(Received via mailing list)
"Ioana Kanda" <kaio4000@gmail.com> writes:

> def human_attribute_name (attr)
>    return case attr
>           when 'email' then 'E-mail:D'
>           else super.human_attribute_name attr
>           end
> end

This seems to be the way to go. There are 2 problems though. First, it
must be a class method, otherwise it won't be found. And second, for
some reason super.human_attribute_name can't be called, I get the
following error:

,----
| undefined method `human_attribute_name' for "Email":String
`----

The following code worked for me:

,----
|   def self.human_attribute_name (attr)
|     return case attr
|       when 'email' then 'E-mail address'
|       # ...
|       else attr.humanize
|     end
|   end
`----

I'll probably make a database table to translate column names to human
names and query it in the above method.

P.S. I still don't understand, why super.human_attribute_name(attr) did
not work instead of attr.humanize.

> But look what I found:
> 
http://wiki.rubyonrails.org/rails/pages/HowToUseVa...
> (good article for me to read it too). Heh

Interesting idea, but not needed in my case, because my model *is*
inherited from ActiveRecord::Base.

--
\  /                                       vlad@hashbang.de
 \/lad                                     http://www.hashbang.de
754b05221455fd8c1f9568030fe953ac?d=identicon&s=25 David Eriksson (2good)
on 2008-02-13 15:00
A much delayed comment to this thread...

Vlad Berditchevskiy wrote:
> P.S. I still don't understand, why super.human_attribute_name(attr) did
> not work instead of attr.humanize.

But this works, right?

ActiveRecord::Base.human_attribute_name(attr)

\David
This topic is locked and can not be replied to.