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.
Navjeet C. (Guest)
on 2006-01-17 03: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
Bakki K. (Guest)
on 2006-01-17 04:04
(Received via mailing list)
validates_presence_of  :fld_name, :message => "Telephone Number cannot
be
empty"
should do it.

-bakki
Wilson B. (Guest)
on 2006-01-17 04:28
(Received via mailing list)
On 1/16/06, Navjeet C. <removed_email_address@domain.invalid> 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.