[Catalyst] best practices for validation

mla maurice.aubrey at gmail.com
Wed May 16 02:17:46 GMT 2007


I want to summarize some of the thoughts on this thread
and see if there's any strong disagreement.

o The model should validate its data.

o The controller should (directly or indirectly) validate
   all the form input so you're pretty sure the model(s)
   can handle it.

o Where the model and the controller validation overlap,
   the validation code should be shared.

o The validation code that runs in the controller is
   responsible for generating the end-user error messages
   and i18n.

o If a validation error makes it to the model, it should
   raise an exception.

o When raising exceptions in the model, use
   Exception:Class so it's catchable. From Dave Rolsky's
   example:

   eval {
      $user->update( %bunch_of_stuff );
   };

   if (my $e = Exception::Class->caught(
         'My::App::Exception::DataValidation') ) {
     # $e->errors contains multiple data validation error messages
     # stuff them in the session
     # save the user's form submission in the session
     # redirect back to form
   } elsif ( my $e = $@ ) {
      die $e;
   }

Anyone think there's a better approach within the context
of large-scale website development?

Thanks,

Maurice



More information about the Catalyst mailing list