Forum: Ruby on Rails change error messages for Validation helpers?

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.
74a56286718bc003d6c7438f7efa1e3a?d=identicon&s=25 Navjeet Chabbewal (jeet)
on 2006-01-17 02:22
Is it possible to change error messages for Validation helpers? I am
writing an app against a existing database (so no control over column
names), but when there is validation error (e.g. with
validate_presence_of) I would like to customize the field name. For
example for telephone whose field name is PhoneNumber I would like to
chnage it to "Telephone Number cannot be empty" rather than "Phonenumber
cannot be empty". Is that possible?

--Jeet
8acba6d057b3f0c2c16cca95ad36881b?d=identicon&s=25 Bakki Kudva (bakki)
on 2006-01-17 03:04
(Received via mailing list)
validates_presence_of  :fld_name, :message => "Telephone Number cannot
be
empty"
should do it.

-bakki
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-01-17 03:28
(Received via mailing list)
On 1/16/06, Navjeet Chabbewal <navjeetc@gmail.com> wrote:
> Is it possible to change error messages for Validation helpers? I am
> writing an app against a existing database (so no control over column
> names), but when there is validation error (e.g. with
> validate_presence_of) I would like to customize the field name. For
> example for telephone whose field name is PhoneNumber I would like to
> chnage it to "Telephone Number cannot be empty" rather than "Phonenumber
> cannot be empty". Is that possible?
>

If you have a limited number of them to override, you can do something
like this, either in your environment.rb, or something that gets
loaded automatically:

module Inflector
  alias_method :humanize_base, :humanize
  def self.humanize(word)
    return "Telephone Number" if word == 'phonenumber'
    humanize_base(word)
  end
end

You could load up your custom 'humanized' attributes from a YAML file,
if you wanted, and check against that list inside humanize.

If you have something more specific in mind, you're better off making
your own 'error_messages_for' helper.  Check out the source for it in
the API documentation. It's quite simple, and is really only there as
a scaffold.
This topic is locked and can not be replied to.