InterJinn works something like this for it's FormJinn engine. So I'd say it sounds quite good :)
Cheers, Rob. On Wed, 2003-10-01 at 19:43, Chris W. Parker wrote: > Hey people. > > I'm trying to come up with an easy way to validate forms. I've got an > idea and I'd like your feedback as well as suggestions and/or your own > methods. > > My idea is to have a multi-dimensional array with fields: data and type. > > Let's say you have a form with three fields. First Name (fname), Last > Name (lname), and Phone Number (phone). It's sent to a processing script > via GET. On the processing script you have the basics: > > <? // pseudo code > > // collect data > $fname = (!empty($_GET['fname'])) ? $_GET['fname'] : "" ; > $lname = (!empty($_GET['lname'])) ? $_GET['lname'] : "" ; > $phone = (!empty($_GET['phone'])) ? $_GET['phone'] : "" ; > > // validate data > if($fname is not [a-zA-Z]{2,20}) > { > // mark as error > } > > if($lname is not [a-zA-Z]{2,20}) > { > // mark as error > } > > $phone = stripOutEverythingButTheNumbers($phone); > > if($phone is not [0-9]{10}) // we only sell to US via website > { > // mark as error > } > > $_SESSION['form_errors'] = put_the_errors_in_here; > > redirect("to_the_previous_page.php"); // where the errors will be read > // and the fields with bad data > // will be marked appropriately > > ?> > > What I'd like to do is get rid of all the if's and what not and throw > them into a function. What I thought of doing to replace all this is: > > <? // pseudo code > > $formdata['fname']['data'] = (!empty($_GET['fname'])) ? $_GET['fname'] : > "" ; > $formdata['fname']['type'] = "name"; > $formdata['lname']['data'] = (!empty($_GET['lname'])) ? $_GET['lname'] : > "" ; > $formdata['lname']['type'] = "name"; > $formdata['phone']['data'] = (!empty($_GET['phone'])) ? $_GET['phone'] : > "" ; > $formdata['phone']['type'] = "phonenumber"; > > > $_SESSION['form_errors'] = validateFormData($formdata); > > > redirect("to_the_previous_page.php"); > > ?> > > The validateFormDate() function would be defined something like this: > > function validateFormData($input) > { > foreach($input as $field) > { > switch ($field['type']) > { > case 'name': > // do the magic > break; > case 'phonenumber': > // do the magic > break; > } > } > } > > > The validateFormData() function will return false if there are no errors > to report. > > For a form that has a lot of fields to validate I think this can make > things pretty easy. > > > So... what do you think? > > > > Chris. > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- .------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php