Change error messages for Validation helpers?


#1

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


#2

validates_presence_of :fld_name, :message => “Telephone Number cannot
be
empty”
should do it.

-bakki


#3

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.