phpDocumentor SForm
[ class tree: SForm ] [ index: SForm ] [ all elements ]

Procedural File: SForm.php

Source Location: /SForm.php

Page Details

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.

  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.

Putting it all together:

    Look in SForm_usage.php for this source

    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, 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.

    Version:  0.1.2
    Filesource:  Source Code for this file
    See:  SForm::__construct()
    Access:  public
    Author:  Seth Price <seth@pricepages.org>
    License:  New BSD License
    Copyright:  Copyright (c) 2006, Seth Price
    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_Maxlenth Set the maximum number of chars in a value
    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

    Documentation generated on Mon, 09 Apr 2007 19:06:54 -0500 by phpDocumentor 1.3.0