Archive for May, 2008
Workflow Apps – Form Validation in PHP
When I work on web applications at my job, one major area to consider is form validation. Some fields may be mandatory, some must only have digits, etc,.
Most of the applications I work on are work flow apps – person X submits form A, members of group Y revise or approve form A, person Z manages group Y and gives final approval for form A, and so on. Because of the many levels of bureaucracy involved, the work flow changes constantly – during development and after.
When my boss told me that he had a new work flow application for me, I wanted to try a new approach by utilizing some object oriented design techniques. The forms of the application are one of things that are changed most often by the many committees overseeing these projects. Taking this into consideration, I developed a form class that would handle form validation with an easy-to-use interface. Here’s an example of it in action (links to the code are at the bottom of the page), and here’s how one would use it:
// create validators
$phonenumChecker = new RegexValueChecker( "/\d/",
array( 7, 10, 11 ), "Phone numbers must have 7, 10, or 11 digits" );
$alphanumChecker = new RegexValueChecker( "/[^a-zA-Z0-9_\s/",
0, "Only alphanumeric characters allowed" );
// create fields
$namefield = new FormField( "Name", $alphanumChecker );
$phonefield = new FormField( "Phone", $phonenumChecker );
$bandfield = new FormField( "Band", $alphanumChecker );
$albumfield = new FormField( "Album", $alphanumChecker );
// form field map collects many fields into one
// (cdfield holds a band name and an album name)
$cdfield = new FormFieldMap( "CD", array( $bandfield, $albumfield ) );
// form field array holds zero to many of one form field
// (cdsfield holds zero to many 'cdfield's)
$cdsfield = new FormFieldArray( "CDs", $cdfield );
// create form
$myform = new Form;
$myform->AddField( $namefield );
$myform->AddField( $phonefield );
$myform->AddField( $cdsfield );
$myform->SetValues( $_GET );
// get errors, if there are any
$ErrorStrings = array( );
$Values = $myform->GetValues( );
$Errors = $myform->GetErrors( $ErrorStrings );
// set up a Smarty template
$Template = new Smarty;
if( $_REQUEST[ "submit" ] ) {
$Template->assign( $Values );
$Template->assign( "Errors", $Errors );
$Template->assign( "ErrorStrings", $ErrorStrings );
}
$Template->display( "index.tpl" );
And it’s just that simple! If you’re not familiar with it, Smarty is a templating engine that will allow you to replace parts of an html file with data. While the form class does not depend on Smarty, it makes using the form class a breeze. The form class also leverages PHP’s ability to store form fields as arrays. For example, if i have two form fields with the same name, (<input name=”a” /> … <input name=”a” />) i can append a pair of square brackets (<input name=”a[]” />) and PHP will see the form field as an array.
The use of FormFieldMap and FormFieldArray might be a little confusing, but their purpose was to support nested sections of forms. I wrote them because the particular app I’m working on has a complex form that has to support a variable number of certain fields. I couldn’t think of any better names for them – I named FormFieldMap after the STL map in C++.
The PHP code is available here, the Smarty template HTML here, and some javascript that it uses here. Hopefully this will improve coding time and maybe cut down input related security vulnerabilities.
2 comments