On 6/7/02 1:04 PM, Perrin Harkins wrote:
> For example, if you have a form for registering as a user which has
> multiple fields, you want to be able to tell them everything that was
> wrong with their input (zip code invalid, phone number invalid, etc.),
> not just the first thing you encountered.
> 
> Putting that into a model object is awkward, since coding your
> constructor or setter methods to keep going after they've found the
> first error feels wrong.  You can write a special validate_input()
> method for it which takes all the input and checks it at once returning
> a list of errors.  You could also just punt and push this out to the
> controller.  (Not very "pure" but simple to implement.)  Either way you
> can use one of the convenient form validation packages on CPAN.

I use my form objects to do this.  For example, to process a "create user"
form, the controller creates the form object:

    $form = My::Form::User::Edit->new;

Initializes it based on the form submission:

    $form->init($form_params);

runs the form object's validate() method:

    unless($form->validate)
    {
      ...

and then (if the validation fails) passes the initialized form objects to
back to the view:

      $app->edit_user_page(form => $form);

There are per-field and per-form errors which are set during the validate()
step.  The view decides where, how, and if to display these.

Going in the reverse direction (say, editing an existing user), the
controller creates the form again:

    $form = My::Form::User::Edit->new;

gets the user object (and handles errors, yadda yadda):

    $user = My::Users->get_user(id => $id) || ...;

initializes the form with the model object:

    $form->init_with_user($user);

and then passes the form to the view:

    $app->edit_user_page(form => $form);

The details have been heavily simplified above, but that's the general idea.
I'm not sure how this all fits into MVC, but it works very well for my
purposes.

-John

Reply via email to