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

Perrin Harkins perrin at elem.com
Tue May 15 20:22:19 GMT 2007


On 5/15/07, mla <maurice.aubrey at gmail.com> wrote:
> And where do you handle the validation? Only in the controller or in
> both the model and controller?

In the form processing code.  This system has a CMS where users get to
generate forms and decide which fields will be on specific forms, so
the required fields depend on the actual form being used.  It could be
separated out better, MVC-wise, but in practice that would be wasted
effort for this system.

There are additional constraints in the database to prevent
accidentally putting in broken data, and a few in perl (with
Params::Validate in the model objects) where they would be difficult
to code in the database.

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

The actual system has a lot of complexity coming from things that
aren't directly related to your question, so it's hard to show a
literal code snip.  The form handling classes are structured so they
follow this basic recipe when a form is submitted:

- Get a Data::FormValidator profile from the form class.
- If the input passes validation:
  - Run the fullfill_request() method for this class.
  - Show the response page.
- If the input fails:
  - Translate the Data::FormValidator results object into error codes
  - Hand the codes to our message class which looks up the local error
text and adds it to the template data.
  - Re-display the form with the messages at the top and the problem
fields highlighted.

This is handled by a base class so in most cases you only need to
write a form profile and a fulfill_request() method to add a new type
of form.

No attempt is made to handle errors that come from the database or
model objects called from fulfill_request().  Errors at that point are
real errors, i.e. unexpected and indicating a bug, not a user input
mistake.  They result in a friendly error screen and a detailed log
message.

- Perrin



More information about the Catalyst mailing list