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?

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

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.

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

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.)

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