/SForm.php

Description

A fast & powerful PEAR_QuickForm replacement

SForm is short for Seth's Form. Why? Because it's easy to remember, and even easier to type. SForm was started out of frusteration with PEAR's QuickForm. It was too slow, it wouldn't allow me to create complex rules, the code was antiquidated, it didn't nest groups, the API was convoluted, the list went on. After spending a few days wrestling with QF, I came to the realization that I could code my own package in the time it took me to figure out how to bend QuickForm to my will. So that's what I did.

SForm automatically does the standard QF things, like JavaScript generation, rendering plugins, both server-side and client side validation, and is very customizable. Some things not included in QF include non- enforced XHTML 1.1 compliance, intrinsic rules, nested groups, and automatic accessibility features like <label> tags, optgroups, JavaScript highlighting of errors, and access keys. I've tried to keep the API very similar to QuickForm, but I've removed things that I found unnecessary or redundant.

SForm is also fast.

The small form that I originally used to develop SForm had a render time of 0.10-0.28 seconds when no form was created. Using QuickForm to build a form on the page increased the render time to 0.33-0.68 secs. Replacing QuickForm with SForm dropped the render time to 0.18-0.42 secs. SForm is almost three times faster than QuickForm in this informal test. (All trials used eAccelerator with caching and optimization enabled.)

Example:

A more detailed example can be found at

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>Testing Seth's Form</title>
  6. </head>
  7. <body>
  8. <?php
  9.  
  10. /**
  11.  * Require SForm
  12.  */
  13. require_once 'SForm.php';
  14.  
  15. //Create a new form
  16. $form = new SForm('subNew');
  17.  
  18. /*
  19.  * Add the data type selection
  20.  */
  21. //Create a new element directly
  22. $dataTypeElm = new SForm_Elm_Select('dataType', 'Data Type');
  23.  
  24. //Add an option and mark it as invalid, but selected by default
  25. $dataTypeElm->addOption('Select a Data Type', -1, array('selected'=>'selected'));
  26. $dataTypeElm->invalidOption(-1);
  27.  
  28. //Add regular options
  29. $dataTypeElm->addOption('Map', 1);
  30. $dataTypeElm->addOption('Observation', 2);
  31.  
  32. //Add a disabled option
  33. $dataTypeElm->addOption('Study', 3, array('disabled'=>'disabled'));
  34.  
  35. //Add the element to the data set in a fieldset
  36. $form->addElement($dataTypeElm);
  37.  
  38. /*
  39.  * Location selection
  40.  */
  41. //Create a new element via a factory method
  42. $areaElm = $form->addElement('select', 'area', 'Area');
  43.  
  44. //Add an option and mark it as invalid
  45. $areaElm->addOption('Select an Area', -1, array('selected'=>'selected'));
  46. $areaElm->invalidOption(-1);
  47.  
  48. //Create options and group them
  49. $areaElm->addOption('Canada', 1);
  50. $areaElm->addOption('United States', 2);
  51. $areaElm->addOption('Mexico', 3);
  52.  
  53. $areaElm->addOptGroup('North America', array(1,2,3));
  54.  
  55. $areaElm->addOption('Brazil', 4);
  56. $areaElm->addOption('Colombia', 5);
  57. $areaElm->addOption('Peru', 6);
  58. $areaElm->addOption('Argentina', 7);
  59.  
  60. $areaElm->addOptGroup('South America', array(4,5,6,7));
  61.  
  62. //These aren't in a group
  63. $areaElm->addOption('Germany', 8);
  64.  
  65. $areaElm->addOption('New Area (see below)', -2);
  66.  
  67. /*
  68.  * Category selection
  69.  */
  70. //Create a fieldset for the checkboxes
  71. $fs = $form->addElement('fieldset', null, 'Category');
  72.  
  73. //Add the checkboxes to the fieldset
  74. $fs->addElement('checkbox', 'forest', 'Forests');
  75. $fs->addElement('checkbox', 'ag', 'Agriculture');
  76. $fs->addElement('checkbox', 'water', 'Water');
  77.  
  78. //Set these to checked by default
  79. $fs->addElement('checkbox', 'climate', 'Climate')->setDefault(true);
  80. $fs->addElement('checkbox', 'health', 'Health', array('checked'=>'checked'));
  81.  
  82. /*
  83.  * Language selection
  84.  */
  85. $langElm = new SForm_Elm_Select('lang', 'Language');
  86.  
  87. $langElm->addOption('English', 'en');
  88. $langElm->addOption('Spanish', 'sp');
  89. $langElm->addOption('Portuguese', 'pr');
  90. $langElm->addOption('German', 'gr');
  91.  
  92. $form->addElement($langElm);
  93.  
  94. /*
  95.  * New location creation fieldset
  96.  */
  97. //Create a fieldset
  98. $fs = new SForm_Fieldset(null, 'New Area');
  99.  
  100. //Add some various input boxes
  101. $fs->addElement($newNameElm = new SForm_Elm_Text('new_name', 'Name'));
  102. $fs->addElement(new SForm_Elm_Text('new_top', 'Top'));
  103. $fs->addElement(new SForm_Elm_Text('new_right', 'Right'));
  104. $fs->addElement(new SForm_Elm_Text('new_bottom', 'Bottom'));
  105. $fs->addElement(new SForm_Elm_Text('new_left', 'Left'));
  106.  
  107. //Create a custom boolean rule
  108. require_once 'SForm/Rule/Boolean.php';
  109. $newNameElm->addRule(new SForm_Rule_Boolean(
  110.             'If you are creating a new area, you must fill in its name.',
  111.             array($newNameElm, $areaElm),
  112.             '%s == "" && %s == -2'));
  113.  
  114. $form->addElement($fs);
  115.  
  116. /*
  117.  * Submission button
  118.  */
  119. $form->addElement('submit', 'sub_new', 'Begin Submission');
  120.  
  121. if($form->validate()){
  122.     $form->freeze();
  123. }
  124.  
  125. $form->display();
  126. ?>
  127. </body>
  128. </html>
.

The SForm API is intentionally very similar to QuickForm. This example was converted directly from the QuickForm documentation. All of the examples on Keith Edmunds's QuickForm tutorial also work with similar modifications. It should be noted that to make this example XHTML 1.1 compliant, the elements would need to be grouped in a fieldset.

  1.  <?php
  2.  // Load the main classes
  3.   require_once 'SForm.php';
  4.  
  5.  // Instantiate the SForm object
  6.   $form = new SForm('firstForm');
  7.  
  8.  // Set defaults for the form elements
  9.   $form->setDefaults(array( 'name' => 'Joe User' ));
  10.  
  11.  // Add some elements to the form
  12.   $form->addElement('header', null, 'SForm tutorial example');
  13.  $form->addElement('text', 'name', 'Enter your name:',
  14.                      array('size' => 50, 'maxlength' => 255));
  15.  $form->addElement('submit', null, 'Send');
  16.  
  17.  // Define filters and validation rules
  18.   $form->applyFilter('name', 'trim');
  19.  $form->addRuleDep('name', 'Please enter your name', 'required', null,'client');
  20.  
  21.  // Try to validate a form
  22.   if ($form->validate()) {
  23.      echo       '<h1>Hello, ' . htmlspecialchars($form->exportValue('name')) . '!</h1>';
  24.      exit;
  25.  }
  26.  
  27.  // Output the form
  28.   $form->display();
  29.  ?>

Note that in the example, addRule() has been changed to addRuleDep(), where 'Dep' stands for depriciated. For more information, see the SForm_Rule documentation.

Important differences with the QuickForm API include:

  • The construtor. The $target attribute has been removed because it breaks compliance with XHTML 1.1.
  • Rule creation and usage. SForm uses a much more object oriented Rule model.
Requirements:

SForm requires at least PHP version 5.1

License:

Copyright (c) 2006, Seth Price <seth@pricepages.org> All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Classes
Class Description
SForm_Element A element is a "leaf" that is actually rendered
SForm_Container A container holds one or more elements
SForm I am the alpha and the omega, I am the form.
SForm_Group Group elements
SForm_Fieldset Group elements in a '<fieldset>'
SForm_Rule Create rules for elements
SForm_Rule_Required Is the given element required?
SForm_Rule_Subset Is the value in (or out of) this subset?
SForm_Renderer Render only the values by default
SForm_Renderer_HTML Render the form to plain (X)HTML
SForm_Renderer_Array Render the form to an array
SForm_Elm_Hidden The hidden element
SForm_Elm_Checkable A base for checkbox or radio elements
SForm_Elm_Checkbox A checkbox element
SForm_Elm_Text A text element
SForm_Elm_Textarea A textarea element
SForm_Elm_Button A button element
SForm_Elm_Reset A reset element
SForm_Elm_Submit A submit element
SForm_Elm_Select A select list
Constants
FORM_ERR_PARENT_EXISTS = 1 (line 136)

FIXME - clean up exception messages and values.

Documentation generated on Wed, 25 Oct 2006 13:03:58 -0500 by phpDocumentor 1.3.0