[Catalyst] Form validation in insert() and update()?

Perrin Harkins perrin at elem.com
Tue May 15 13:32:50 GMT 2007


On 5/14/07, Dave Rolsky <autarch at urth.org> wrote:
>   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;
>   }

Although there's no real problem I know of with using exceptions this
way, I've gotten away from using them for expected user input
mistakes.  There's no need for the flow control they provide in this
kind of scenario, since you're going to keep going long enough to
collect all the input mistakes anyway.  It makes more sense to me to
just use a separate validate call and a normal data structure for this
and save exceptions for times when you actually need to bail out of
something.

Right now, I just check the input with Data::Form::Validator and
convert the results it returns into error message names, which are
then mapped to strings for display.  The mapping allows the messages
to be overridden for a specific form (this code uses
Class::PhraseBook) so that they can be context-specific.

- Perrin



More information about the Catalyst mailing list