Hello, Any idea how to remove attribute name from validation error messages? thanks, bogumbiker
ranjan kumar
2009-Sep-26 10:50 UTC
Re: removing attribute prefix from validation error messages
Try this class User < ActiveRecord::Base validate do |user| user.errors.add_to_base("This is my custom message") if user. attribute_name.blank? end end On Sat, Sep 26, 2009 at 2:52 PM, bgumbiker <bogumil.bialous-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> > Hello, > Any idea how to remove attribute name from validation error messages? > thanks, > bogumbiker > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
richardun
2009-Sep-26 13:20 UTC
Re: removing attribute prefix from validation error messages
Actually, bgumbiker, I just posted this answer yesterday so a bit more searching might have produced an answer. http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/cf59e09f98981f27/6c7fe5ff9dd016bc?lnk=gst&q=customize+active+record+attribute+display+name+in+error+message#6c7fe5ff9dd016bc If you used Ranjan''s answer, you''ll have to enter a specific custom message for each validation you do. If you use the solution I outlined in that URL or below, you change the "humanization" version of that column name so you can do validations as normal and when the human name is used to display an error message, it''ll use the one you mapped. class Post < ActiveRecord::Base HUMANIZED_COLUMNS = {:msg => "Message"} def self.human_attribute_name(attribute) HUMANIZED_COLUMNS[attribute.to_sym] || super end end Richard On Sep 26, 5:50 am, ranjan kumar <ranjankumar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Try this > > class User < ActiveRecord::Base > validate do |user| > user.errors.add_to_base("This is my custom message") if user. > attribute_name.blank? > end > end > > On Sat, Sep 26, 2009 at 2:52 PM, bgumbiker <bogumil.bial...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote: > > > > > Hello, > > Any idea how to remove attribute name from validation error messages? > > thanks, > > bogumbiker > >
bgumbiker
2009-Sep-26 21:52 UTC
Re: removing attribute prefix from validation error messages
THanks Richard, Could you post any further usage example as I am quite new in Ruby and Rails and do not understand what happening below in your code. Is :msg an attribute in the model? thanks, bogumbiker On Sep 26, 11:20 pm, richardun <richar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Actually, bgumbiker, I just posted this answer yesterday so a bit more > searching might have produced an answer.http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/... > > If you used Ranjan''s answer, you''ll have to enter a specific custom > message for each validation you do. If you use the solution I > outlined in that URL or below, you change the "humanization" version > of that column name so you can do validations as normal and when the > human name is used to display an error message, it''ll use the one you > mapped. > > class Post < ActiveRecord::Base > > HUMANIZED_COLUMNS = {:msg => "Message"} > > def self.human_attribute_name(attribute) > HUMANIZED_COLUMNS[attribute.to_sym] || super > end > end > > Richard > > On Sep 26, 5:50 am, ranjan kumar <ranjankumar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Try this > > > class User < ActiveRecord::Base > > validate do |user| > > user.errors.add_to_base("This is my custom message") if user. > > attribute_name.blank? > > end > > end > > > On Sat, Sep 26, 2009 at 2:52 PM, bgumbiker <bogumil.bial...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote: > > > > Hello, > > > Any idea how to remove attribute name from validation error messages? > > > thanks, > > > bogumbiker
richardun
2009-Sep-27 22:28 UTC
Re: removing attribute prefix from validation error messages
Your validation error message is coming from your model. In your model, you have something like this: class MyModel < ActiveRecord::Base ... validates_presence_of :msg, :message => "can''t be blank" ... end When you submit a form you are validating that the message someone enters in the column field you created in your database, which is called "msg" actually has something in it (validates_presence_of). Here, we''re saying when someone submits a form and the "msg" field is empty, give them this error: <name of column> can''t be blank The name of the column here is "msg." But you don''t want to display that in your error back to the client. You want it to say something nice like "Message can''t be blank" not "msg can''t be blank." So, in my method, we take the same model we have above and add the humanize mapping. class MyModel < ActiveRecord::Base ... validates_presence_of :msg, :message => "can''t be blank" HUMANIZED_COLUMNS = {:msg => "Message"} def self.human_attribute_name(attribute) HUMANIZED_COLUMNS[attribute.to_sym] || super end end With that method, you''re just mapping a hash of symbols (in this case you just have one - :msg) and modifying the human_attribute_name method that rails uses when displaying the validation error. You see, when the validation fails (the value for the field was not present) it spits out the error containing the (humanized version) of the column name with the :message you gave it. The validation error is always humanizing the column name, but it can''t make much of "msg" so it leaves it alone. I always suggest giving column names (in your database / model) when you set it up to be good column names it can humanize. But, if you have to have one like "msg," then you can work around it when it tries to humanize it. To break down the method just a little, "human_attribute_name" is a method in ActionRecord, but it''s present here since your class "MyModel" inherits from ActionRecord::Base. You are essentially modifying it by "def"ining it here. You''re just saying, when you "humanize" a column, for whatever reason, see if the column name is in my hash "HUMANIZED_COLUMNS" and use that value instead of the column name you''re going to try to humanize. Hope that helps, Richard On Sep 26, 4:52 pm, bgumbiker <bogumil.bial...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> THanks Richard, > Could you post any further usage example as I am quite new in Ruby and > Rails and do not understand what happening below in your code. Is :msg > anattributein the model? > thanks, > bogumbiker > > On Sep 26, 11:20 pm, richardun <richar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Actually, bgumbiker, I just posted this answer yesterday so a bit more > > searching might have produced an answer.http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/... > > > If you used Ranjan''s answer, you''ll have to enter a specific custom > > message for eachvalidationyou do. If you use the solution I > > outlined in that URL or below, you change the "humanization" version > > of that column name so you can do validations as normal and when the > > human name is used to display anerrormessage, it''ll use the one you > > mapped. > > > class Post < ActiveRecord::Base > > > HUMANIZED_COLUMNS = {:msg => "Message"} > > > def self.human_attribute_name(attribute) > > HUMANIZED_COLUMNS[attribute.to_sym] || super > > end > > end > > > Richard > > > On Sep 26, 5:50 am, ranjan kumar <ranjankumar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Try this > > > > class User < ActiveRecord::Base > > > validate do |user| > > > user.errors.add_to_base("This is my custom message") if user. > > > attribute_name.blank? > > > end > > > end > > > > On Sat, Sep 26, 2009 at 2:52 PM, bgumbiker <bogumil.bial...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote: > > > > > Hello, > > > > Any idea how to removeattributename fromvalidationerrormessages? > > > > thanks, > > > > bogumbiker > >
richardun
2009-Sep-27 22:29 UTC
Re: removing attribute prefix from validation error messages
"Yes" is the short answer, but I was hoping that explaining more about it you''d get a little more out of my answer. Cheers, Richard On Sep 27, 5:28 pm, richardun <richar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Yourvalidationerrormessage is coming from your model. In your > model, you have something like this: > > class MyModel < ActiveRecord::Base > > ... > > validates_presence_of :msg, :message => "can''t be blank" > > ... > > end > > When you submit a form you are validating that the message someone > enters in the column field you created in your database, which is > called "msg" actually has something in it (validates_presence_of). > Here, we''re saying when someone submits a form and the "msg" field is > empty, give them thiserror: > > <name of column> can''t be blank > > The name of the column here is "msg." But you don''t want to display > that in yourerrorback to the client. You want it to say something > nice like "Message can''t be blank" not "msg can''t be blank." > > So, in my method, we take the same model we have above and add the > humanize mapping. > > class MyModel < ActiveRecord::Base > > ... > > validates_presence_of :msg, :message => "can''t be blank" > > HUMANIZED_COLUMNS = {:msg => "Message"} > > def self.human_attribute_name(attribute) > HUMANIZED_COLUMNS[attribute.to_sym] || super > end > > end > > With that method, you''re just mapping a hash of symbols (in this case > you just have one - :msg) and modifying the human_attribute_name > method that rails uses when displaying thevalidationerror. You see, > when thevalidationfails (the value for the field was not present) it > spits out theerrorcontaining the (humanized version) of the column > name with the :message you gave it. Thevalidationerroris always > humanizing the column name, but it can''t make much of "msg" so it > leaves it alone. I always suggest giving column names (in your > database / model) when you set it up to be good column names it can > humanize. But, if you have to have one like "msg," then you can work > around it when it tries to humanize it. > > To break down the method just a little, "human_attribute_name" is a > method in ActionRecord, but it''s present here since your class > "MyModel" inherits from ActionRecord::Base. You are essentially > modifying it by "def"ining it here. You''re just saying, when you > "humanize" a column, for whatever reason, see if the column name is in > my hash "HUMANIZED_COLUMNS" and use that value instead of the column > name you''re going to try to humanize. > > Hope that helps, > Richard > > On Sep 26, 4:52 pm, bgumbiker <bogumil.bial...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > THanks Richard, > > Could you post any further usage example as I am quite new in Ruby and > > Rails and do not understand what happening below in your code. Is :msg > > anattributein the model? > > thanks, > > bogumbiker > > > On Sep 26, 11:20 pm, richardun <richar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Actually, bgumbiker, I just posted this answer yesterday so a bit more > > > searching might have produced an answer.http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/... > > > > If you used Ranjan''s answer, you''ll have to enter a specific custom > > > message for eachvalidationyou do. If you use the solution I > > > outlined in that URL or below, you change the "humanization" version > > > of that column name so you can do validations as normal and when the > > > human name is used to display anerrormessage, it''ll use the one you > > > mapped. > > > > class Post < ActiveRecord::Base > > > > HUMANIZED_COLUMNS = {:msg => "Message"} > > > > def self.human_attribute_name(attribute) > > > HUMANIZED_COLUMNS[attribute.to_sym] || super > > > end > > > end > > > > Richard > > > > On Sep 26, 5:50 am, ranjan kumar <ranjankumar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Try this > > > > > class User < ActiveRecord::Base > > > > validate do |user| > > > > user.errors.add_to_base("This is my custom message") if user. > > > > attribute_name.blank? > > > > end > > > > end > > > > > On Sat, Sep 26, 2009 at 2:52 PM, bgumbiker <bogumil.bial...@gmail.com>wrote: > > > > > > Hello, > > > > > Any idea how to removeattributename fromvalidationerrormessages? > > > > > thanks, > > > > > bogumbiker > >
bgumbiker
2009-Sep-27 23:58 UTC
Re: removing attribute prefix from validation error messages
Thanks Richard!, It is now clear I like it! bogum On Sep 28, 8:29 am, richardun <richar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> "Yes" is the short answer, but I was hoping that explaining more about > it you''d get a little more out of my answer. > > Cheers, > Richard > > On Sep 27, 5:28 pm, richardun <richar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Yourvalidationerrormessage is coming from your model. In your > > model, you have something like this: > > > class MyModel < ActiveRecord::Base > > > ... > > > validates_presence_of :msg, :message => "can''t be blank" > > > ... > > > end > > > When you submit a form you are validating that the message someone > > enters in the column field you created in your database, which is > > called "msg" actually has something in it (validates_presence_of). > > Here, we''re saying when someone submits a form and the "msg" field is > > empty, give them thiserror: > > > <name of column> can''t be blank > > > The name of the column here is "msg." But you don''t want to display > > that in yourerrorback to the client. You want it to say something > > nice like "Message can''t be blank" not "msg can''t be blank." > > > So, in my method, we take the same model we have above and add the > > humanize mapping. > > > class MyModel < ActiveRecord::Base > > > ... > > > validates_presence_of :msg, :message => "can''t be blank" > > > HUMANIZED_COLUMNS = {:msg => "Message"} > > > def self.human_attribute_name(attribute) > > HUMANIZED_COLUMNS[attribute.to_sym] || super > > end > > > end > > > With that method, you''re just mapping a hash of symbols (in this case > > you just have one - :msg) and modifying the human_attribute_name > > method that rails uses when displaying thevalidationerror. You see, > > when thevalidationfails (the value for the field was not present) it > > spits out theerrorcontaining the (humanized version) of the column > > name with the :message you gave it. Thevalidationerroris always > > humanizing the column name, but it can''t make much of "msg" so it > > leaves it alone. I always suggest giving column names (in your > > database / model) when you set it up to be good column names it can > > humanize. But, if you have to have one like "msg," then you can work > > around it when it tries to humanize it. > > > To break down the method just a little, "human_attribute_name" is a > > method in ActionRecord, but it''s present here since your class > > "MyModel" inherits from ActionRecord::Base. You are essentially > > modifying it by "def"ining it here. You''re just saying, when you > > "humanize" a column, for whatever reason, see if the column name is in > > my hash "HUMANIZED_COLUMNS" and use that value instead of the column > > name you''re going to try to humanize. > > > Hope that helps, > > Richard > > > On Sep 26, 4:52 pm, bgumbiker <bogumil.bial...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > THanks Richard, > > > Could you post any further usage example as I am quite new in Ruby and > > > Rails and do not understand what happening below in your code. Is :msg > > > anattributein the model? > > > thanks, > > > bogumbiker > > > > On Sep 26, 11:20 pm, richardun <richar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Actually, bgumbiker, I just posted this answer yesterday so a bit more > > > > searching might have produced an answer.http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/... > > > > > If you used Ranjan''s answer, you''ll have to enter a specific custom > > > > message for eachvalidationyou do. If you use the solution I > > > > outlined in that URL or below, you change the "humanization" version > > > > of that column name so you can do validations as normal and when the > > > > human name is used to display anerrormessage, it''ll use the one you > > > > mapped. > > > > > class Post < ActiveRecord::Base > > > > > HUMANIZED_COLUMNS = {:msg => "Message"} > > > > > def self.human_attribute_name(attribute) > > > > HUMANIZED_COLUMNS[attribute.to_sym] || super > > > > end > > > > end > > > > > Richard > > > > > On Sep 26, 5:50 am, ranjan kumar <ranjankumar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Try this > > > > > > class User < ActiveRecord::Base > > > > > validate do |user| > > > > > user.errors.add_to_base("This is my custom message") if user. > > > > > attribute_name.blank? > > > > > end > > > > > end > > > > > > On Sat, Sep 26, 2009 at 2:52 PM, bgumbiker <bogumil.bial...@gmail.com>wrote: > > > > > > > Hello, > > > > > > Any idea how to removeattributename fromvalidationerrormessages? > > > > > > thanks, > > > > > > bogumbiker