Hey, I have validation that I need to be able to bypass depending on a cookie that the user might have. My problem is for most users, they have to have a .edu or .com email address. So I wrote that into the validation. But if a certain cookie is set, I want to allow them to register with any email address, and still run the other validations (uniqueness of email, etc.) Does anyone have any information on how to do this? USER MODEL validates_presence_of :first_name, :last_name, :email, :password validates_uniqueness_of :email validates_format_of :email, :with => %r{\.(edu|com)$}i, :message => "Must be a valid email address" -- 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 -~----------~----~----~----~------~----~------~--~---
On 18 Dec 2007, at 14:09, Joe Peck wrote:> > Hey, > > I have validation that I need to be able to bypass depending on a > cookie > that the user might have. > > My problem is for most users, they have to have a .edu or .com email > address. So I wrote that into the validation. > > But if a certain cookie is set, I want to allow them to register with > any email address, and still run the other validations (uniqueness of > email, etc.) > > Does anyone have any information on how to do this? > > USER MODEL > validates_presence_of :first_name, :last_name, :email, :password > validates_uniqueness_of :email > validates_format_of :email, > :with => %r{\.(edu|com)$}i, > :message => "Must be a valid email address" > --validates_format_of :email, :with => %r{\.(edu|com)$}i, :message => "Must be a valid email address", :if => :some_method Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 18 Dec 2007, at 14:09, Joe Peck wrote: > >> But if a certain cookie is set, I want to allow them to register with >> :message => "Must be a valid email address" >> -- > > validates_format_of :email, > :with => %r{\.(edu|com)$}i, > :message => "Must be a valid email address", > :if => :some_method > > FredHmm, what kind of method would work in there? I tried validates_format_of :email, :with => %r{\.(edu|com)$}i, :message => "Must be a valid email address", :if => cookies[:is_admin] != "YES" and also have tried validates_format_of :email, :with => %r{\.(edu|com)$}i, :message => "Must be a valid email address", :if => not_admin def not_admin if cookies[:is_admin] == "YES" false else true end end But I get errors from both of them. How can I check for a cookie in the validation code? -- 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 -~----------~----~----~----~------~----~------~--~---
On 18 Dec 2007, at 15:24, Joe Peck wrote:> > Frederick Cheung wrote: >> On 18 Dec 2007, at 14:09, Joe Peck wrote: >> >>> But if a certain cookie is set, I want to allow them to register >>> with >>> :message => "Must be a valid email address" >>> -- >> >> validates_format_of :email, >> :with => %r{\.(edu|com)$}i, >> :message => "Must be a valid email address", >> :if => :some_method >> >> Fred > > Hmm, what kind of method would work in there? I tried >[snip]> > But I get errors from both of them. How can I check for a cookie in > the > validation code?Cookies are controller only. You will somehow have the let the model know that it''s ok to skip the validation (eg set an attribute). Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> > Cookies are controller only. You will somehow have the let the model > know that it''s ok to skip the validation (eg set an attribute). > > FredHmm, so there is no way to check for a cookie in the validation? That would make things so much easier... Is there a way to check for the cookie in the controller and then pass through a parameter so that certain validation is skipped? Something like this in the controller, but make it only skip certain validation instead of all: if cookies[:is_admin] == "YES" user.save(false) else user.save end -- 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 -~----------~----~----~----~------~----~------~--~---
On 18 Dec 2007, at 16:30, Joe Peck wrote:> >> >> Cookies are controller only. You will somehow have the let the model >> know that it''s ok to skip the validation (eg set an attribute). >> >> Fred > Hmm, so there is no way to check for a cookie in the validation? That > would make things so much easier... > > Is there a way to check for the cookie in the controller and then pass > through a parameter so that certain validation is skipped? Something > like this in the controller, but make it only skip certain validation > instead of all: >I''ve done similar things with something like class User attr_accessor :extended_signup validates ... :if => :extended_signup end at this point user.extended_signup = true user.save runs the validation user.extended_signup = false user.save doesn''t Fred> if cookies[:is_admin] == "YES" > user.save(false) > else > user.save > end > -- > 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 -~----------~----~----~----~------~----~------~--~---