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?
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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.