Old Echo
2007-Dec-31 21:51 UTC
Validation order when mixing callbacks and validates_format
I posted this question the other day, but it was pretty late so I think I wrote a bunch of gibberish. So let me try it now that I''ve been properly doused with caffeine. :) The Problem: I''m using the "before_validation" callback to modify an attribute value on a particular model object before it gets validated. I''m also using "validates_format_of" to do error checking on that same model attribute. If "before_validation" doesn''t happen before "validates_format_of", Rails blows up. Looking in my AWDWR book, it looks like when you call model.save(), there is an order to validation: before_validation ... after_validation ... before_save before_create / before_update INSERT / UPDATE OPERATION ... However, when I try to create a new model object, it looks like before_validation does not get called before "validates_format_of". My assumption would be that the "validates_" helpers should before after "before_validation", but this doesn''t seem to be correct. Can anyone help me out with this? When does "validates_format_of" get called in relation to "before_validation"? Thanks, kodama -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
brewpoo
2007-Dec-31 22:45 UTC
Re: Validation order when mixing callbacks and validates_format
Can you post your before_validation code? What actually blows up? Can you also post the relevant lines from the log? On Dec 31, 4:51 pm, Old Echo <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I posted this question the other day, but it was pretty late so I think > I wrote a bunch of gibberish. So let me try it now that I''ve been > properly doused with caffeine. :) > > The Problem: I''m using the "before_validation" callback to modify an > attribute value on a particular model object before it gets validated. > I''m also using "validates_format_of" to do error checking on that same > model attribute. If "before_validation" doesn''t happen before > "validates_format_of", Rails blows up. > > Looking in my AWDWR book, it looks like when you call model.save(), > there is an order to validation: > > before_validation > ... > after_validation > ... > before_save > before_create / before_update > INSERT / UPDATE OPERATION > ... > > However, when I try to create a new model object, it looks like > before_validation does not get called before "validates_format_of". My > assumption would be that the "validates_" helpers should before after > "before_validation", but this doesn''t seem to be correct. > > Can anyone help me out with this? When does "validates_format_of" get > called in relation to "before_validation"? > > Thanks, > kodama > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Marli Ba
2008-Oct-28 19:23 UTC
Re: Validation order when mixing callbacks and validates_format
brewpoo wrote:> Can you post your before_validation code? > > What actually blows up? Can you also post the relevant lines from the > log?I am having this same problem. It seems the before_validation isn''t working in mine: before_validation :normalize_phone_number validates_format_of :number, :with => /^[0-9]{10,15}$/ protected def normalize_phone_number self.number.gsub(/-/,"").gsub(/ /,"") end The object won''t save since it fails the validates_format_of since it never runs the before_validation. I''m running Rails 2.1.0. Any ideas? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Marli Ba
2008-Oct-28 20:47 UTC
Re: Validation order when mixing callbacks and validates_format
I just figured out what I was doing wrong. I was using "gsub" instead of "gsub!" now it is working fine -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jim Rowe
2009-Jun-19 11:06 UTC
Re: Validation order when mixing callbacks and validates_format
I''m having the same problem. I want to allow people to enter a value including a $ or £. With this method, the value of the attribute in before_validation is zero if I enter a value with text (or a $) at the start. So entering $1000 is turned into 0, before before_validation. Any ideas? Marli Ba wrote:> brewpoo wrote: >> Can you post your before_validation code? >> >> What actually blows up? Can you also post the relevant lines from the >> log? > > I am having this same problem. It seems the before_validation isn''t > working in mine: > > > before_validation :normalize_phone_number > > validates_format_of :number, :with => /^[0-9]{10,15}$/ > > protected > > def normalize_phone_number > self.number.gsub(/-/,"").gsub(/ /,"") > end > > > The object won''t save since it fails the validates_format_of since it > never runs the before_validation. I''m running Rails 2.1.0. > > Any ideas?-- Posted via http://www.ruby-forum.com/.