Hi, I need to selectively turn off validations for example, I want to make sure that password and password_confirmation fields are equal when user first signs up or changes his password from his profile, but not in other cases e.g. updates his profile. right now I am using a quick and dirty hack by using booleans e.g. if(@skip_password_validations != false) #--do validations --# is there anything better than this. any help would be greatly appreciated. -- Ishaq http://blogs.kahaf.com/ishaq - Programming can be fun, so can be cryptography, however they should not be combined ... --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 2/11/08, Muhammad Ishaq <ishaq.malik-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I need to selectively turn off validations for example, I want to > make sure that password and password_confirmation fields are equal when user > first signs up or changes his password from his profile, but not in other > cases e.g. updates his profile. > > right now I am using a quick and dirty hack by using booleans e.g. > if(@skip_password_validations != false) #--do validations --# > > is there anything better than this. any help would be greatly > appreciated.Something like this: validates_presence_of :password, :unless => :skip_password_validations? validates_presence_of :password_confirmation, unless => :skip_password_validations? validates_confirmation_of :password, unless => :skip_password_validations? def skip_password_validations? @skip_password_validations end -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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 -~----------~----~----~----~------~----~------~--~---
On 11 Feb 2008, at 14:15, Rick DeNatale wrote:> > On 2/11/08, Muhammad Ishaq <ishaq.malik-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Hi, >> >> I need to selectively turn off validations for example, I >> want to >> make sure that password and password_confirmation fields are equal >> when user >> first signs up or changes his password from his profile, but not in >> other >> cases e.g. updates his profile. >> >> right now I am using a quick and dirty hack by using booleans >> e.g. >> if(@skip_password_validations != false) #--do validations --# >> >> is there anything better than this. any help would be greatly >> appreciated. > > Something like this: > > validates_presence_of :password, :unless => > :skip_password_validations? > validates_presence_of :password_confirmation, unless => > :skip_password_validations? > validates_confirmation_of :password, unless => > :skip_password_validations? > > def skip_password_validations? > @skip_password_validations > endor even with_options :unless => skip_password_validations? do validates_presence_of :password validates_presence_of :password_confirmation validates_confirmation_of :password end> > > -- > Rick DeNatale > > My blog on Ruby > http://talklikeaduck.denhaven2.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 -~----------~----~----~----~------~----~------~--~---
Muhammad Ishaq wrote:> e.g. > if(@skip_password_validations != false) #--do validations --# > > is there anything better than this. any help would be greatly > appreciated.What I''m doing looks like this: validates_format_of :password, :with => /^\w+$/, :message => ''<b style = "color:red">Password must be alphanumeric</b>'', :on => :save validates_length_of :password, :minimum => 5, :message => ''<b style = "color:red">Password to short (Must be 5 characters long)</b>'', :on => :create -- 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 -~----------~----~----~----~------~----~------~--~---
Thanks a lot, that really helps cleaning the code a little On Feb 11, 2008 7:55 PM, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On 11 Feb 2008, at 14:15, Rick DeNatale wrote: > > > > > On 2/11/08, Muhammad Ishaq <ishaq.malik-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Hi, > >> > >> I need to selectively turn off validations for example, I > >> want to > >> make sure that password and password_confirmation fields are equal > >> when user > >> first signs up or changes his password from his profile, but not in > >> other > >> cases e.g. updates his profile. > >> > >> right now I am using a quick and dirty hack by using booleans > >> e.g. > >> if(@skip_password_validations != false) #--do validations --# > >> > >> is there anything better than this. any help would be greatly > >> appreciated. > > > > Something like this: > > > > validates_presence_of :password, :unless => > > :skip_password_validations? > > validates_presence_of :password_confirmation, unless => > > :skip_password_validations? > > validates_confirmation_of :password, unless => > > :skip_password_validations? > > > > def skip_password_validations? > > @skip_password_validations > > end > > or even > with_options :unless => skip_password_validations? do > validates_presence_of :password > validates_presence_of :password_confirmation > validates_confirmation_of :password > end > > > > > > > > -- > > Rick DeNatale > > > > My blog on Ruby > > http://talklikeaduck.denhaven2.com/ > > > > > > > > > >-- Ishaq http://blogs.kahaf.com/ishaq - Programming can be fun, so can be cryptography, however they should not be combined ... --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
What about the :if parameter that validations can take? Like this: validates_format_of :password, :with => /^\w+$/, :message => ''<b style ="color:red">Password must be alphanumeric</ b>'', :on => :save :if => @skip_password_validations The parameter can even be a Proc! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---