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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Testing Seth's Form</title>
</head>
<body>
<?php
/**
* Require SForm
*/
require_once 'SForm.php';
//Create a new form
$form =
new SForm('subNew');
/*
* Add the data type selection
*/
//Create a new element directly
//Add an option and mark it as invalid, but selected by default
$dataTypeElm->addOption('Select a Data Type', -1, array('selected'=>'selected'));
$dataTypeElm->invalidOption(-1);
//Add regular options
$dataTypeElm->addOption('Map', 1);
$dataTypeElm->addOption('Observation', 2);
//Add a disabled option
$dataTypeElm->addOption('Study', 3, array('disabled'=>'disabled'));
//Add the element to the data set in a fieldset
$form->addElement($dataTypeElm);
/*
* Location selection
*/
//Create a new element via a factory method
$areaElm = $form->addElement('select', 'area', 'Area');
//Add an option and mark it as invalid
$areaElm->addOption('Select an Area', -1, array('selected'=>'selected'));
$areaElm->invalidOption(-1);
//Create options and group them
$areaElm->addOption('Canada', 1);
$areaElm->addOption('United States', 2);
$areaElm->addOption('Mexico', 3);
$areaElm->addOptGroup('North America', array(1,2,3));
$areaElm->addOption('Brazil', 4);
$areaElm->addOption('Colombia', 5);
$areaElm->addOption('Peru', 6);
$areaElm->addOption('Argentina', 7);
$areaElm->addOptGroup('South America', array(4,5,6,7));
//These aren't in a group
$areaElm->addOption('Germany', 8);
$areaElm->addOption('New Area (see below)', -2);
/*
* Category selection
*/
//Create a fieldset for the checkboxes
$fs = $form->addElement('fieldset', null, 'Category');
//Add the checkboxes to the fieldset
$fs->addElement('checkbox', 'forest', 'Forests');
$fs->addElement('checkbox', 'ag', 'Agriculture');
$fs->addElement('checkbox', 'water', 'Water');
//Set these to checked by default
$fs->addElement('checkbox', 'climate', 'Climate')->setDefault(true);
$fs->addElement('checkbox', 'health', 'Health', array('checked'=>'checked'));
/*
* Language selection
*/
$langElm->addOption('English', 'en');
$langElm->addOption('Spanish', 'sp');
$langElm->addOption('Portuguese', 'pr');
$langElm->addOption('German', 'gr');
$form->addElement($langElm);
/*
* New location creation fieldset
*/
//Create a fieldset
//Add some various input boxes
$fs->addElement($newNameElm =
new SForm_Elm_Text('new_name', 'Name'));
//Create a custom boolean rule
require_once 'SForm/Rule/Boolean.php';
'If you are creating a new area, you must fill in its name.',
array($newNameElm, $areaElm),
'%s == "" && %s == -2'));
$form->addElement($fs);
/*
* Submission button
*/
$form->addElement('submit', 'sub_new', 'Begin Submission');
if($form->validate()){
$form->freeze();
}
$form->display();
?>
</body>
</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.
<?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.
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.