Validation

Form validation is a tedious thing. In fact, it is so common and tedious, you might think that the form validation problem would have been solved a long time ago, and that now it’s a matter of dropping in a solution in a few lines. That’s not quite true.

Early versions of Pox used SmartyValidate and Zend_Form. Each had undesirable features. SmartyValidate's was actually not too bad except for one glaring flaw – it saved the form in the session. This became a problem when a user had the same form open in two windows. It became a problem with the back button. It became a problem with two forms on one page. Zend_Form rendered too much HTML for us. It mixed too much presentation logic with business logic. Experience has taught us that if you are dealing with even the minimalist design, you will end up digging through the framework code, looking for exactly where that HTML is generated. The decorators feature just seemed like too much work.

Pox is designed so that the web designer can design the form in any way he wishes, with a simple way to mark up validation errors for developers. Coders are able to code up the form, validate it, and handle success or failure in one place. We ended up using Zend_Filter_Input, which pushes any data you give it through a set of specified validators and filters. With this, you can pass it the form input directly, and it would pass or fail the form data depending on the validators set up by the coder.

Here is the Pox Form class. You can read about its usage in the blog tutorial on the page about creating a blog.

Security

Web security generally has one mantra:

Filter input, escape output.

We wanted security to be an integral part of forms. The idea was to force developers to think about filtering input, and to make it more inconvenient to make something insecure. The Inspekt library does this. If you give it data, it will destroy the original, and create a “cage” around the data. Then, the only way to access the data is via an Inspekt filter method. This encourages thinking about filtering all input, and not using unfiltered input (though that is still possible).

In the Form class, you can see that when a form passes, its input is given to Inspekt, destroying the original $_POST data. Then the Inspekt filters must be used to access the form data.

pox-php/forms_validation_security.txt · Last modified: 2010/05/12 20:29 by gerard
 
 
© 2010 Straylightrun.net under Creative Commons Attribution
Green hosting by Dreamhost.com | Powered by DokuWiki