# Validation in a model that is not to be saved

**URL:** <https://www.ruby-forum.com/t/validation-in-a-model-that-is-not-to-be-saved/79906>\
**Category:** Rails\
**Created:** [December 20, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/validation-in-a-model-that-is-not-to-be-saved/79906 "2006-12-20T16:51:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason1706](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@Jason1706](https://www.ruby-forum.com/u/Jason1706)\
**Post date:** [December 20, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/validation-in-a-model-that-is-not-to-be-saved/79906/1 "2006-12-20T16:51:11Z")

</div>

Hi group,

I built an input form that’s spread over multiple pages. I store the  
already given data (that match my models and my tables) in a cart-like  
object. That works so far. But since I dont save the data, when the  
user inputs it, validations don’t work. They may work on my last  
(submit) page, but that too late.

Any way to make validations work even if they don’t pass the model?

Thx in advance  
Jason

---

<div class="post-metadata">

**Author:** ![Jason1706](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@Jason1706](https://www.ruby-forum.com/u/Jason1706)\
**Post date:** [December 20, 2006, 5:01pm UTC](https://www.ruby-forum.com/t/validation-in-a-model-that-is-not-to-be-saved/79906/2 "2006-12-20T17:01:15Z")

</div>

Hello Jason,

2006/12/20, Jason1706 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Any way to make validations work even if they don’t pass the model?

If you have real ActiveRecord objects, you can call #valid?

[http://api.rubyonrails.com/classes/ActiveRecord/Validations.html#M000800](http://api.rubyonrails.com/classes/ActiveRecord/Validations.html#M000800)

## Hope that helps !

François Beausoleil  
[http://blog.teksol.info/](http://blog.teksol.info/)  
[http://piston.rubyforge.org/](http://piston.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![Jason1706](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@Jason1706](https://www.ruby-forum.com/u/Jason1706)\
**Post date:** [December 20, 2006, 5:03pm UTC](https://www.ruby-forum.com/t/validation-in-a-model-that-is-not-to-be-saved/79906/3 "2006-12-20T17:03:18Z")

</div>

I think you need to get the attributes of the object assigned like

@object = Model.new(session[:cart])  
@object.additional\_attribute = value  
@object.additional\_attribute = value

if @object.valid?  
…  
end

I _think_ something like that will work.

Adam

---

<div class="post-metadata">

**Author:** ![Jason1706](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@Jason1706](https://www.ruby-forum.com/u/Jason1706)\
**Post date:** [December 20, 2006, 8:15pm UTC](https://www.ruby-forum.com/t/validation-in-a-model-that-is-not-to-be-saved/79906/4 "2006-12-20T20:15:25Z")

</div>

Hi François,

thx for your hint. So far, I was able to add basic validation to my  
model like that

def validate  
errors.add\_on\_empty %w{ title description }  
end

And in my controller where I temporarily store that object in my  
virtual cart I can probe on validity like that :

def create  
@product = Product.new(params[:product])  
if @product.valid?  
if @cart = find\_cart  
@cart.add\_product(@product)  
redirect\_to(:action =\> ‘display\_cart’)  
else  
render :action =\> ‘new’  
end  
else  
redirect\_to(:action =\> ‘new’)  
end  
end

But that great validation features for forms are still closed for me at  
that point. Anything I dont see? This way, I’d to to all that by hand  
… and I’m a lazy typist 🙂

Cheers Jason

---

<div class="post-metadata">

**Author:** ![Jason1706](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@Jason1706](https://www.ruby-forum.com/u/Jason1706)\
**Post date:** [December 23, 2006, 8:24am UTC](https://www.ruby-forum.com/t/validation-in-a-model-that-is-not-to-be-saved/79906/5 "2006-12-23T08:24:30Z")

</div>

this will be your cup of tea:

[http://www.realityforge.org/articles/2005/12/02/validations-for-non-activerecord-model-objects](http://www.realityforge.org/articles/2005/12/02/validations-for-non-activerecord-model-objects)

---

<div class="post-metadata">

**Author:** ![Jason1706](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@Jason1706](https://www.ruby-forum.com/u/Jason1706)\
**Post date:** [December 23, 2006, 8:43am UTC](https://www.ruby-forum.com/t/validation-in-a-model-that-is-not-to-be-saved/79906/6 "2006-12-23T08:43:14Z")

</div>

I think the question Jason is asking is how does he validate the  
individual fields even though the model isn’t fully baked (because of  
the multi-page form). Is that correct?

Validating for presence would be difficult in this situation because you  
have maybe three items that need to be present on the first page, two on  
the second, and two on the last. But the model instance won’t validate  
unless they’re all there.

Here’s an idea. What if you store a ‘step’ attribute in the model, and  
only validate if you have reached the proper step? So the controller  
sets the step attribute to ‘2’ after step 2 and then you put in the  
model:

validates\_presence\_of :foo, :if =\> Proc.new { |model| !model.step \>= 2 }

Validating format would be similar. You can say something like:

validates\_length\_of :foo, :within =\> 5…10,  
:if =\> Proc.new { |model| !model.foo.blank? }

That way the validation won’t fire unless the field has data.

Does that help?

---

<div class="post-metadata">

**Author:** ![Jason1706](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@Jason1706](https://www.ruby-forum.com/u/Jason1706)\
**Post date:** [December 23, 2006, 8:46am UTC](https://www.ruby-forum.com/t/validation-in-a-model-that-is-not-to-be-saved/79906/7 "2006-12-23T08:46:46Z")

</div>

Adam B. wrote:

> validates\_presence\_of :foo, :if =\> Proc.new { |model| !model.step \>= 2 }

Whoops. No negation needed there. Should read:

validates\_presence\_of :foo, :if =\> Proc.new { |model| model.step \>= 2 }

And where I said ‘model’ I was being generic. To be consistent with your  
previous example, ‘model’ should be ‘product’ I guess:

class Product  
validates\_presence\_of :foo, :if =\> Proc.new { |p| p.step \>= 2 }  
validates\_length\_of :foo, :within =\> 5…10,  
:if =\> Proc.new { |p| !p.foo.blank? }  
end

/afb
