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

class Model < ActiveRecord::Base
attr_accessor :validation_stage

validates_presence_of :something, :if =>{|m|
m.validation_stage == :first}
validates_confirmation_of :else, :if =>{|m|
m.validation_stage == :second}

In your controller you can do something along the lines of:

class WizardController < ActionController::Base
def step1
if request.method == :post
@model =[:model])
@model.validation_stage = :first

    redirect_to step2_url(@model)

@model ||=


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

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 26 Mar 2008, at 16:23, lunaclaire wrote:


Sure, you can just stick :if => :my_method


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


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 C. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs