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

mla maurice.aubrey at gmail.com
Tue May 15 19:42:52 GMT 2007


Perrin Harkins wrote:
> 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.

And where do you handle the validation? Only in the controller or in
both the model and controller?

Could you give a short example of taking an actual field from the 
request parameters, validating it, and updating a row with it?

Thanks,

Maurice



More information about the Catalyst mailing list