Navjeet Chabbewal
2006-Jan-17 00:46 UTC
[Rails] change error messages for Validation helpers?
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 -- Posted via http://www.ruby-forum.com/.
validates_presence_of :fld_name, :message => "Telephone Number cannot be empty" should do it. -bakki 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? > > --Jeet > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060117/0ff0bda6/attachment.html
Wilson Bilkovich
2006-Jan-17 01:51 UTC
[Rails] change error messages for Validation helpers?
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.