Categorized | News

Form Handling Simplified

Posted on 03 April 2004 by Demian Turner

My apologies to Chris but if you’re still using ‘glorious’ code like this (see the add function) to solve the form problem, you’re jumping through hoops you don’t have too. 

While the easyform example has a great look & feel, clean XHTML layout and makes good use of fieldsets, some developers may find the PHP code that drives it difficult to maintain.  An OO approach like the one used in Seagull makes things a lot easier:

  • PHP and HTML are kept in separate layers like most OO projects
  • the Request object takes responsibility for all request cleanup
    • whitespace stripping
    • javascript removal
    • offsetting the effects of magic_quotes
    • removal of html if required
  • the validate methods implemented by all manager classes enforce strict variable assignment, so only the vars you specify from GET/POST/COOKIE are assigned to the input object
  • any number of validation tests you specify, from non-empty value checking to regex testing, must be passed before the input object is considered valid
  • all form variable persistence is taken care of by the validate/process/display workflow: all input that fails at least one validation test is sent straight to output, bypassing the process method where all data manipulation is carried out, effectively repopulating the form with all the user’s values, including preselecting combobox elements, ticking checkboxes, radios, etc
  • context sensitive error messages are fed back to the user

See some of the code examples for a brief overview, try the demo (Register is a good example) or download the latest version of Seagull and try it out.

Bookmark and Share

2 Comments For This Post

  1. efarmboy Says:

    This looks good but is it possible to use the form classes without the complete framework?

    I have an existing library and framework but would like to try these OO classes for creating and managing the forms.

  2. demian Says:

    hi efarmboy

    well, the validate/process/display workflow is enforced by the application controller class, you wouldn\’t really be able to cut and paste anything i\’m afraid. there is very little code that\’s form related, it\’s the workflow approach that makes things easier.



Leave a Reply



Demian Turner's currently-reading book recommendations, reviews, favorite quotes, book clubs, book trivia, book lists



PHPkitchen recommends you also check out the following sites :

Accounting for Small Businesses

FreeAgent sign-up