PHP DevCenter
oreilly.comSafari Books Online.Conferences.


Quick and Clean PHP Forms
Pages: 1, 2, 3

The Complete Implementation

At this point, it should be clear that these new wrapper functions could be logically implemented within a subclass of HTML_QuickForm, that would in turn make the contact form code cleaner still. The subclass, called HTML_QuickerForm, is implemented in a file of the same name:

  class HTML_QuickerForm extends HTML_QuickForm {
    function HTML_QuickerForm($formName='', 
                              $trackSubmit = false) {
       $this->applyFilter('__ALL__', 'trim');
    function addDefaultMessages($default_messages) {
      $this->default_messages = $default_messages;
    // replaces underscores with spaces 
    // and capitalizes the resulting words
    function createFieldLabel($field_name) {
      return ucwords(ereg_replace('[_]', ' ', $field_name));
    function addQuickRules($field_name, 
                           $validation_methods) {
      if ($validation_methods != '') {
        $methods = split(',', $validation_methods);
        foreach ($methods as $method) {
          $validation_message = sprintf($this->default_messages[$method], ereg_replace('[_]', ' ', $field_name));
    function addQuickElement($field_name, 
                             $attributes='') {
    function executeForm() {    
      if ($this->validate()) {
        $this->process("process_data", false);
      } else {

First, notice that the constructor has been overridden to automatically apply the trim filter to all form fields by default, a best practice learned from Keith Edmunds' tutorial. The $default_messages variable is now an instance variable instead of a global, and I renamed the functions so that, as methods of this subclass, they will not override the similarly named existing implementations in the super class they were meant to wrap. I also added the ability to add attributes to form elements to round out the functionality.

Further subclasses could be defined for common implementations of the process method callback. Since emailing the validated form data is a typical requirement of contact forms, this can be implemented in a subclass of HTML_QuickerForm with a small amount of PHP reflection trickery:

class HTML_QuickMailForm extends HTML_QuickerForm {
    function executeForm() {    
    if ($this->validate()) {
      $this->process(array(get_class($this), 'process_data'), false);
    } else {
  function process_data($values) {
      require_once "emailHelper.php";

The email contact form can now be rewritten in a way that succinctly specifies only what is necessary for this particular form implementation:

Example 2.

$form = new HTML_QuickMailForm('contact_form', 'post');

$default_messages = array ('required' => 'Please enter your %s.',
                            'email'    => 'Please enter a valid %s.'

$form->addQuickElement('name',          'text',     'required',      'class=name'); 
$form->addQuickElement('address',       'text'      ); 
$form->addQuickElement('email_address', 'text',     'required,email'); 
$form->addQuickElement('home_phone',    'text',     'required'      );
$form->addQuickElement('cell_phone',    'text'      ); 
$form->addQuickElement('subject',       'text'      ); 

$textareaAttributes = array("wrap" => "virtual", 
                            "cols" => "60"
$form->addQuickElement('message',       'textarea', '', $textareaAttributes); 
$form->addQuickElement('submit',        'submit'    ); 

This version of the contact form actually contains more functionality, because more validations have been added, as have element attributes, for the sake of giving a more complete example of how to use the new subclass. For a given form element, multiple validation methods can be specified using comma separated strings, as shown by the email_address field, and multiple attributes can be specified using an array of appropriate values, as shown by the message field. In each case, single strings can also be used when only one value is needed, as shown by the name field which specifies only one validation method and one attribute.

Thanks to the power of HTML_QuickForm, and a little time spent personalizing how it may be used typically, this is all the coding needed to create an email contact form in the future.

Pages: 1, 2, 3

Next Pagearrow

Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!

Sponsored by: