Forum: Ruby on Rails model validation across multiple views

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Shelby Westman (Guest)
on 2006-06-13 03:40
(Received via mailing list)
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
Phillip Kast (Guest)
on 2006-06-13 03:47
(Received via mailing list)
Shelby Westman <shelby.westman@gmail.com> 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
Bill Walton (Guest)
on 2006-06-13 03:56
(Received via mailing list)
Hi Shelby,

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

hth,
Bill
Shelby Westman (Guest)
on 2006-06-13 04:12
(Received via mailing list)
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
Phillip Kast (Guest)
on 2006-06-13 04:15
(Received via mailing list)
Bill Walton <bill.walton@charter.net> 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
Phillip Kast (Guest)
on 2006-06-13 04:18
(Received via mailing list)
Shelby Westman <shelby.westman@gmail.com> 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
Rails@lists.rubyonrails.org
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
Bill Walton (Guest)
on 2006-06-13 04:21
(Received via mailing list)
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 Westman
  To: rails@lists.rubyonrails.org
  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
  Rails@lists.rubyonrails.org
  http://lists.rubyonrails.org/mailman/listinfo/rails
97a76353906ed4aa0dfc36a7de8776c3?d=identicon&s=25 Jodi S. (jshow)
on 2006-06-13 04:25
(Received via mailing list)
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
This topic is locked and can not be replied to.