# Model validation across multiple views

**URL:** <https://www.ruby-forum.com/t/model-validation-across-multiple-views/63193>\
**Category:** Rails\
**Created:** [June 13, 2006, 3:40am UTC](https://www.ruby-forum.com/t/model-validation-across-multiple-views/63193 "2006-06-13T03:40:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shelby\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@Shelby\_W](https://www.ruby-forum.com/u/Shelby_W)\
**Post date:** [June 13, 2006, 3:40am UTC](https://www.ruby-forum.com/t/model-validation-across-multiple-views/63193/1 "2006-06-13T03:40:54Z")

</div>

I have a rather complex object with a number of attributes. For a  
variety  
of reasons, I would like one view to create the object with only a  
couple of  
fields completed, then a second and third view to finish all the fields.  
I  
would like the model to validate\_presence\_of all these fields, since  
eventually I need them all there, and I would like each page to validate  
its  
portion of the fields that that view presents.

How do I best do that? The only thing I can think of is some kind of  
use of  
single table inheritance, with each child object filling in a portion of  
the  
fields of the total. But that seems weird. Does anyone have a better  
idea?

Thanks for any advice,  
Shelby

---

<div class="post-metadata">

**Author:** ![Shelby\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@Shelby\_W](https://www.ruby-forum.com/u/Shelby_W)\
**Post date:** [June 13, 2006, 3:47am UTC](https://www.ruby-forum.com/t/model-validation-across-multiple-views/63193/2 "2006-06-13T03:47:10Z")

</div>

Shelby W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote: I have a rather complex  
object with a number of attributes. For a variety of reasons, I would  
like one view to create the object with only a couple of fields  
completed, then a second and third view to finish all the fields. I  
would like the model to validate\_presence\_of all these fields, since  
eventually I need them all there, and I would like each page to validate  
its portion of the fields that that view presents.

How do I best do that? The only thing I can think of is some kind of  
use of single table inheritance, with each child object filling in a  
portion of the fields of the total. But that seems weird. Does anyone  
have a better idea?

Thanks for any advice,  
Shelby

As long as your object is serializable (it probably is), you can just  
instantiate your model, fill in whatever you’ve got so far, slap that  
sucker in the session and pull it back out in the next step.

hth,  
phil

---

<div class="post-metadata">

**Author:** ![Shelby\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@Shelby\_W](https://www.ruby-forum.com/u/Shelby_W)\
**Post date:** [June 13, 2006, 4:12am UTC](https://www.ruby-forum.com/t/model-validation-across-multiple-views/63193/3 "2006-06-13T04:12:03Z")

</div>

Both:

Object.save\_with\_validation(false) might give you the flexibility you’re

> looking for.

and putting the object it in the session would work. But neither would  
allow me to validate only the portion of the object that I fill in with  
each  
of the views that I use to gradually complete filling in all the fields  
of  
the object.

Shelby

---

<div class="post-metadata">

**Author:** ![Shelby\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@Shelby\_W](https://www.ruby-forum.com/u/Shelby_W)\
**Post date:** [June 13, 2006, 4:15am UTC](https://www.ruby-forum.com/t/model-validation-across-multiple-views/63193/4 "2006-06-13T04:15:13Z")

</div>

Bill W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
Object.save\_with\_validation(false) might give you the flexibility  
you’re looking for.

The trouble in this case is you save a lot of invalid rows into your  
database – what if somebody bounces before completing the process? So  
you then need some way of dealing with the invalid rows and deciding  
whether they should get deleted yet, or if they are still in process.

cheers,  
phil

---

<div class="post-metadata">

**Author:** ![Shelby\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@Shelby\_W](https://www.ruby-forum.com/u/Shelby_W)\
**Post date:** [June 13, 2006, 3:56am UTC](https://www.ruby-forum.com/t/model-validation-across-multiple-views/63193/5 "2006-06-13T03:56:28Z")

</div>

Hi Shelby,

Object.save\_with\_validation(false) might give you the flexibility you’re  
looking for.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![Shelby\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@Shelby\_W](https://www.ruby-forum.com/u/Shelby_W)\
**Post date:** [June 13, 2006, 4:18am UTC](https://www.ruby-forum.com/t/model-validation-across-multiple-views/63193/6 "2006-06-13T04:18:23Z")

</div>

Shelby W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote: Both:

Object.save\_with\_validation (false) might give you the flexibility  
you’re looking for.

and putting the object it in the session would work. But neither would  
allow me to validate only the portion of the object that I fill in with  
each of the views that I use to gradually complete filling in all the  
fields of the object.

Shelby

* * *

Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
Ahhh… good point. You can check if individual fields have errors like  
so:

@foo = Foo.new(params[:foo])  
@foo.valid? #No, cause we haven’t finished completing it yet!  
@foo.errors.invalid? :phone # =\> true if phone is invalid

There might be an even cleaner way, but I haven’t found it yet if there  
is.  
-phil

---

<div class="post-metadata">

**Author:** ![Shelby\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@Shelby\_W](https://www.ruby-forum.com/u/Shelby_W)\
**Post date:** [June 13, 2006, 4:25am UTC](https://www.ruby-forum.com/t/model-validation-across-multiple-views/63193/7 "2006-06-13T04:25:13Z")

</div>

search the mailing list for Wizards(damn gmane is down again,  
otherwise I’d send you a reference)

general idea is to use a stage (input type hidden) and then validate  
particular attributes based it,

ala,

#stage 1 entry validation  
validates\_presence\_of :patient\_id, :if =\> :after\_stage\_one, :message  
=\> " must be selected"

#stage 2 entry validation  
validates\_presence\_of :age, :if =\> :at\_stage\_two, :message =\> " must  
be entered"  
validates\_numericality\_of :age, :if =\> :at\_stage\_two, :only\_integer  
=\> true, :message =\> " must be numeric"

def at\_stage\_one  
stage == 1  
end

I use this method for a 20+ entry screen. Works great.

In your controller then,

if model.valid?

```
 model.save

```

else  
render :partial =\> errors  
end

let me know if I can be more verbose.

Cheers,  
Jodi

---

<div class="post-metadata">

**Author:** ![Shelby\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19b73/32.png) [@Shelby\_W](https://www.ruby-forum.com/u/Shelby_W)\
**Post date:** [June 13, 2006, 4:21am UTC](https://www.ruby-forum.com/t/model-validation-across-multiple-views/63193/8 "2006-06-13T04:21:52Z")

</div>

Absolutely right. As is Phillip. Server-side validation has it’s  
limitations. You might want to consider JS validation on the  
client-side.

hth,  
Bill  
----- Original Message -----  
From: Shelby W.  
To: removed\_email\_address@domain.invalid  
Sent: Monday, June 12, 2006 9:09 PM  
Subject: Re: [Rails] model validation across multiple views

Both:

```
Object.save_with_validation (false) might give you the flexibility 

```

you’re looking for.

and putting the object it in the session would work. But neither  
would allow me to validate only the portion of the object that I fill in  
with each of the views that I use to gradually complete filling in all  
the fields of the object.

Shelby

* * *

* * *

Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
