[Catalyst] "Best Practice" for validating form input

Thomas L Shinnick tshinnic at io.com
Mon Aug 1 22:56:55 CEST 2005


At 15:19 8/1/2005, Sean Davis wrote:
>I am a new user to Catalyst, but have done a few small projects using
>CGI::Application.  In that framework, I tended to not map by URL to page but
>used the runmode parameter coded into forms.  I am now beginning to see the
>power of URL mapping and wondered what other folks are doing when a form is
>submitted.  
>
>In CGI::App, I typically had a form submit back to itself and them used
>something like ->forward (an "internal redirect") to move to the next page
>if the form validated.  Using URL mapping, one needs to do a real redirect.
>I'm just curious what peoples' thoughts are on an idiom that works for
>Catalyst, as the two-queries per page (one to collect data, and the second
>to do a redirect after the data is validated) seems slightly tedious for
>many cases.

One small example of a pattern is shown in the ServerDB example.
  (see Catalyst Wiki catalyst.perl.org front page, section Examples,
    which links to http://www.hybridized.org/catalyst/)

If you look in the source for its ServerDB::C::Server controller,
you will see two actions, 'view' and 'do_edit'.  'view' is used to
display the form (template at root/server/view.xhtml).  The form
references the submit action 'do_edit'.  'do_edit' has the code
which _could_ do the validation of form values.  If there were
errors it could just as easily do what it already does after
processing correct values, just forward (not redirect) to action
'view' to redisplay the form:
    $c->forward('view', join "/", @name);

If you will notice in some of the other examples and documentation,
including the plugin FillInForm in the application class
    use Catalyst qw{  -Debug FillInForm ... };
means that forms easily and automatically (and spookily) have the
prior form fields value filled in.  That is, just redisplaying the
form will show the problem values!

So anyway, with the forward() capability, it is easy to have one
action that displays forms, and another that validates values
submitted.  If there is an error, just forward to the display
action and return from the submit action.  At least, that's _my_ theory. ;)


>Thanks,
>Sean





More information about the Catalyst mailing list