lunaclaire
2008-Mar-21 02:09 UTC
validating parts of a model step-by-step in a wizard flow
I have multistep wizard path for creation of a model and I''d like to validate each page as the user moves forward. Each page let''s the user build up parts of the model with attributes that need to be validated. In the controller, I make a call to update_attributes with the params from that page, but there are attributes we haven''t gotten to yet on following pages that fail the validation because they haven''t been filled in yet. A kludge that might address this is to set some attr flag on the model from the controller that would be checked in the model''s validate method so that only the appropriate attributes would be checked at each step, but that doesn''t seem very clean. Is there a best practices way that this kind of thing is handled that I''ve missed in my reading? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Andrew Bloom
2008-Mar-21 02:51 UTC
Re: validating parts of a model step-by-step in a wizard flow
The AR validation methods support an :if option. Try something like this: class Model < ActiveRecord::Base attr_accessor :validation_stage validates_presence_of :something, :if => Proc.new{|m| m.validation_stage == :first} validates_confirmation_of :else, :if => Proc.new{|m| m.validation_stage == :second} end In your controller you can do something along the lines of: class WizardController < ActionController::Base def step1 if request.method == :post @model = Model.new(params[:model]) @model.validation_stage = :first if @model.save redirect_to step2_url(@model) end end @model ||= Model.new end end On Mar 20, 9:09 pm, lunaclaire <szager...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have multistep wizard path for creation of a model and I''d like to > validate each page as the user moves forward. Each page let''s the user > build up parts of the model with attributes that need to be validated. > > In the controller, I make a call to update_attributes with the params > from that page, but there are attributes we haven''t gotten to yet on > following pages that fail the validation because they haven''t been > filled in yet. > > A kludge that might address this is to set some attr flag on the model > from the controller that would be checked in the model''s validate > method so that only the appropriate attributes would be checked at > each step, but that doesn''t seem very clean. > > Is there a best practices way that this kind of thing is handled that > I''ve missed in my reading?--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
lunaclaire
2008-Mar-26 16:23 UTC
Re: validating parts of a model step-by-step in a wizard flow
Thanks, Andrew! I''m on vacation, so I apologize for the delay in responding... I''m having trouble finding docs on use of :if with validation helpers, but this looks like it would work... maybe you can pt me to some docs. I ask because I wonder if it''s necessary to do the check in a Proc block like that or if I can just have the logic (I dont know why, but I''m not crazy about the Proc style) or can call a method from the :if statement. Funny, but while waiting to see a reply to my posting, I''d already come up with validation_stage (named it the same, too), but put it in a validate method. But the conciseness of validation helpers is nice. On Mar 20, 7:51 pm, Andrew Bloom <akbl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The AR validation methods support an :if option. Try something like > this: > > class Model < ActiveRecord::Base > attr_accessor :validation_stage > > validates_presence_of :something, :if => Proc.new{|m| > m.validation_stage == :first} > validates_confirmation_of :else, :if => Proc.new{|m| > m.validation_stage == :second} > end > > In your controller you can do something along the lines of: > > class WizardController < ActionController::Base > def step1 > if request.method == :post > @model = Model.new(params[:model]) > @model.validation_stage = :first > > if @model.save > redirect_to step2_url(@model) > end > end > > @model ||= Model.new > end > end > > On Mar 20, 9:09 pm, lunaclaire <szager...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I have multistep wizard path for creation of a model and I''d like to > > validate each page as the user moves forward. Each page let''s the user > > build up parts of the model with attributes that need to be validated. > > > In the controller, I make a call to update_attributes with the params > > from that page, but there are attributes we haven''t gotten to yet on > > following pages that fail the validation because they haven''t been > > filled in yet. > > > A kludge that might address this is to set some attr flag on the model > > from the controller that would be checked in the model''s validate > > method so that only the appropriate attributes would be checked at > > each step, but that doesn''t seem very clean. > > > Is there a best practices way that this kind of thing is handled that > > I''ve missed in my reading?--~--~---------~--~----~------------~-------~--~----~ 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
2008-Mar-26 16:29 UTC
Re: validating parts of a model step-by-step in a wizard flow
On 26 Mar 2008, at 16:23, lunaclaire wrote:> > Thanks, Andrew! > > I''m on vacation, so I apologize for the delay in responding... > > I''m having trouble finding docs on use of :if with validation helpers, > but this looks like it would work... maybe you can pt me to some docs. > I ask because I wonder if it''s necessary to do the check in a Proc > block like that or if I can just have the logic (I dont know why, but > I''m not crazy about the Proc style) or can call a method from the :if > statement. >Sure, you can just stick :if => :my_method Fred> Funny, but while waiting to see a reply to my posting, I''d already > come up with validation_stage (named it the same, too), but put it in > a validate method. But the conciseness of validation helpers is nice. > > > On Mar 20, 7:51 pm, Andrew Bloom <akbl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> The AR validation methods support an :if option. Try something like >> this: >> >> class Model < ActiveRecord::Base >> attr_accessor :validation_stage >> >> validates_presence_of :something, :if => Proc.new{|m| >> m.validation_stage == :first} >> validates_confirmation_of :else, :if => Proc.new{|m| >> m.validation_stage == :second} >> end >> >> In your controller you can do something along the lines of: >> >> class WizardController < ActionController::Base >> def step1 >> if request.method == :post >> @model = Model.new(params[:model]) >> @model.validation_stage = :first >> >> if @model.save >> redirect_to step2_url(@model) >> end >> end >> >> @model ||= Model.new >> end >> end >> >> On Mar 20, 9:09 pm, lunaclaire <szager...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >>> I have multistep wizard path for creation of a model and I''d like to >>> validate each page as the user moves forward. Each page let''s the >>> user >>> build up parts of the model with attributes that need to be >>> validated. >> >>> In the controller, I make a call to update_attributes with the >>> params >>> from that page, but there are attributes we haven''t gotten to yet on >>> following pages that fail the validation because they haven''t been >>> filled in yet. >> >>> A kludge that might address this is to set some attr flag on the >>> model >>> from the controller that would be checked in the model''s validate >>> method so that only the appropriate attributes would be checked at >>> each step, but that doesn''t seem very clean. >> >>> Is there a best practices way that this kind of thing is handled >>> that >>> I''ve missed in my reading? > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
lunaclaire
2008-Mar-26 16:39 UTC
Re: validating parts of a model step-by-step in a wizard flow
Thanks, Fred. Any ptrs to where to find docs for the "if clause in these helpers? (BTW - You''re a great resource on this list. I''ve noticed how often you respond to those of us begging for help. Thanks for that.) On Mar 26, 9:29 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 26 Mar 2008, at 16:23, lunaclaire wrote: > > > > > Thanks, Andrew! > > > I''m on vacation, so I apologize for the delay in responding... > > > I''m having trouble finding docs on use of :if with validation helpers, > > but this looks like it would work... maybe you can pt me to some docs. > > I ask because I wonder if it''s necessary to do the check in a Proc > > block like that or if I can just have the logic (I dont know why, but > > I''m not crazy about the Proc style) or can call a method from the :if > > statement. > > Sure, you can just stick :if => :my_method > > Fred > > > Funny, but while waiting to see a reply to my posting, I''d already > > come up with validation_stage (named it the same, too), but put it in > > a validate method. But the conciseness of validation helpers is nice. > > > On Mar 20, 7:51 pm, Andrew Bloom <akbl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> The AR validation methods support an :if option. Try something like > >> this: > > >> class Model < ActiveRecord::Base > >> attr_accessor :validation_stage > > >> validates_presence_of :something, :if => Proc.new{|m| > >> m.validation_stage == :first} > >> validates_confirmation_of :else, :if => Proc.new{|m| > >> m.validation_stage == :second} > >> end > > >> In your controller you can do something along the lines of: > > >> class WizardController < ActionController::Base > >> def step1 > >> if request.method == :post > >> @model = Model.new(params[:model]) > >> @model.validation_stage = :first > > >> if @model.save > >> redirect_to step2_url(@model) > >> end > >> end > > >> @model ||= Model.new > >> end > >> end > > >> On Mar 20, 9:09 pm, lunaclaire <szager...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >>> I have multistep wizard path for creation of a model and I''d like to > >>> validate each page as the user moves forward. Each page let''s the > >>> user > >>> build up parts of the model with attributes that need to be > >>> validated. > > >>> In the controller, I make a call to update_attributes with the > >>> params > >>> from that page, but there are attributes we haven''t gotten to yet on > >>> following pages that fail the validation because they haven''t been > >>> filled in yet. > > >>> A kludge that might address this is to set some attr flag on the > >>> model > >>> from the controller that would be checked in the model''s validate > >>> method so that only the appropriate attributes would be checked at > >>> each step, but that doesn''t seem very clean. > > >>> Is there a best practices way that this kind of thing is handled > >>> that > >>> I''ve missed in my reading?--~--~---------~--~----~------------~-------~--~----~ 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
2008-Mar-26 17:03 UTC
Re: validating parts of a model step-by-step in a wizard flow
On 26 Mar 2008, at 16:39, lunaclaire wrote:> > Thanks, Fred. > > Any ptrs to where to find docs for the "if clause in these helpers? >Well it''s in the rdoc for all of the validation helpers Fred> (BTW - You''re a great resource on this list. I''ve noticed how often > you respond to those of us begging for help. Thanks for that.) > > On Mar 26, 9:29 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> On 26 Mar 2008, at 16:23, lunaclaire wrote: >> >> >> >>> Thanks, Andrew! >> >>> I''m on vacation, so I apologize for the delay in responding... >> >>> I''m having trouble finding docs on use of :if with validation >>> helpers, >>> but this looks like it would work... maybe you can pt me to some >>> docs. >>> I ask because I wonder if it''s necessary to do the check in a Proc >>> block like that or if I can just have the logic (I dont know why, >>> but >>> I''m not crazy about the Proc style) or can call a method from >>> the :if >>> statement. >> >> Sure, you can just stick :if => :my_method >> >> Fred >> >>> Funny, but while waiting to see a reply to my posting, I''d already >>> come up with validation_stage (named it the same, too), but put it >>> in >>> a validate method. But the conciseness of validation helpers is >>> nice. >> >>> On Mar 20, 7:51 pm, Andrew Bloom <akbl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>>> The AR validation methods support an :if option. Try something like >>>> this: >> >>>> class Model < ActiveRecord::Base >>>> attr_accessor :validation_stage >> >>>> validates_presence_of :something, :if => Proc.new{|m| >>>> m.validation_stage == :first} >>>> validates_confirmation_of :else, :if => Proc.new{|m| >>>> m.validation_stage == :second} >>>> end >> >>>> In your controller you can do something along the lines of: >> >>>> class WizardController < ActionController::Base >>>> def step1 >>>> if request.method == :post >>>> @model = Model.new(params[:model]) >>>> @model.validation_stage = :first >> >>>> if @model.save >>>> redirect_to step2_url(@model) >>>> end >>>> end >> >>>> @model ||= Model.new >>>> end >>>> end >> >>>> On Mar 20, 9:09 pm, lunaclaire <szager...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >>>>> I have multistep wizard path for creation of a model and I''d >>>>> like to >>>>> validate each page as the user moves forward. Each page let''s the >>>>> user >>>>> build up parts of the model with attributes that need to be >>>>> validated. >> >>>>> In the controller, I make a call to update_attributes with the >>>>> params >>>>> from that page, but there are attributes we haven''t gotten to >>>>> yet on >>>>> following pages that fail the validation because they haven''t been >>>>> filled in yet. >> >>>>> A kludge that might address this is to set some attr flag on the >>>>> model >>>>> from the controller that would be checked in the model''s validate >>>>> method so that only the appropriate attributes would be checked at >>>>> each step, but that doesn''t seem very clean. >> >>>>> Is there a best practices way that this kind of thing is handled >>>>> that >>>>> I''ve missed in my reading? > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---