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:
The SForm API is intentionally very similar to QuickForm. This example was converted directly from the html.html-quickform.tutorial.php 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.
<?php
// Load the main classes
require_once 'SForm.php';
// Instantiate the SForm object
$form =
new SForm('firstForm');
// Set defaults for the form elements
$form->setDefaults(array( 'name' => 'Joe User' ));
// Add some elements to the form
$form->addElement('header', null, 'SForm tutorial example');
$form->addElement('text', 'name', 'Enter your name:',
array('size' => 50, 'maxlength' => 255));
$form->addElement('submit', null, 'Send');
// Define filters and validation rules
$form->applyFilter('name', 'trim');
$form->addRuleDep('name', 'Please enter your name', 'required', null,'client');
// Try to validate a form
if ($form->validate()) {
exit;
}
// Output the form
$form->display();
?>
Note that in the example, addRule() has been changed to addRuleDep(), where 'Dep' stands for depriciated. For more information, see the SForm_Rule documentation.
Putting it all together:
Look in SForm_usage.php for this sourceImportant 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, but version 5.1 is recommended.
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.