Model validation across multiple views

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

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

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

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

Hi Shelby,

Object.save_with_validation(false) might give you the flexibility you’re
looking for.

hth,
Bill

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

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

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: [email protected]
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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails