# Validating parts of a model step-by-step in a wizard flow

**URL:** <https://www.ruby-forum.com/t/validating-parts-of-a-model-step-by-step-in-a-wizard-flow/132530>\
**Category:** Rails\
**Created:** [March 21, 2008, 3:10am UTC](https://www.ruby-forum.com/t/validating-parts-of-a-model-step-by-step-in-a-wizard-flow/132530 "2008-03-21T03:10:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [March 21, 2008, 3:10am UTC](https://www.ruby-forum.com/t/validating-parts-of-a-model-step-by-step-in-a-wizard-flow/132530/1 "2008-03-21T03:10:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [March 21, 2008, 3:51am UTC](https://www.ruby-forum.com/t/validating-parts-of-a-model-step-by-step-in-a-wizard-flow/132530/2 "2008-03-21T03:51:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [March 26, 2008, 5:25pm UTC](https://www.ruby-forum.com/t/validating-parts-of-a-model-step-by-step-in-a-wizard-flow/132530/3 "2008-03-26T17:25:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [March 26, 2008, 5:30pm UTC](https://www.ruby-forum.com/t/validating-parts-of-a-model-step-by-step-in-a-wizard-flow/132530/4 "2008-03-26T17:30:41Z")

</div>

On 26 Mar 2008, at 16:23, lunaclaire wrote:

> statement.

Sure, you can just stick :if =\> :my\_method

Fred

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [March 26, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/validating-parts-of-a-model-step-by-step-in-a-wizard-flow/132530/5 "2008-03-26T18:05:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [March 26, 2008, 5:40pm UTC](https://www.ruby-forum.com/t/validating-parts-of-a-model-step-by-step-in-a-wizard-flow/132530/6 "2008-03-26T17:40:21Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
