Source for file QuickForm.php
Documentation is available at QuickForm.php
require_once 'HTML/QuickForm.php';
* US-English messages for some QuickForm rules. Moritz Heidkamp
* suggested this approach for easier i18n.
if (! isset ($GLOBALS['_DB_TABLE']['qf_rules'])) {
$GLOBALS['_DB_TABLE']['qf_rules'] = array (
'required' => 'The item %s is required.',
'numeric' => 'The item %s must be numbers only.',
'maxlength' => 'The item %s can have no more than %d characters.'
* DB_Table_QuickForm creates HTML_QuickForm objects from DB_Table properties.
* DB_Table_QuickForm provides HTML form creation facilities based on
* DB_Table column definitions transformed into HTML_QuickForm elements.
* $Id: QuickForm.php,v 1.11 2004/07/12 15:14:22 pmjones Exp $
* @author Paul M. Jones <pmjones@ciaweb.net>
* Build a form based on DB_Table column definitions.
* @param array $cols A sequential array of DB_Table column definitions
* from which to create form elements.
* @param string $arrayName By default, the form will use the names
* of the columns as the names of the form elements. If you pass
* $arrayName, the column names will become keys in an array named
* @param array $args An associative array of optional arguments to
* pass to the QuickForm object. The keys are...
* 'formName' : String, name of the form; defaults to the name of the
* 'method' : String, form method; defaults to 'post'.
* 'action' : String, form action; defaults to
* $_SERVER['REQUEST_URI'].
* 'target' : String, form target target; defaults to '_self'
* 'attributes' : Associative array, extra attributes for <form>
* tag; the key is the attribute name and the value is attribute
* 'trackSubmit' : Boolean, whether to track if the form was
* submitted by adding a special hidden field
* @param string $clientValidate By default, validation will match
* the 'qf_client' value from the column definition. However,
* if you set $clientValidate to true or false, this will
* override the value from the column definition.
* @return object HTML_QuickForm
function &getForm($cols, $arrayName = null , $args = array (),
$formName = isset ($args['formName'])
? $args['formName'] : $this->table;
$method = isset ($args['method'])
? $args['method'] : 'post';
$action = isset ($args['action'])
? $args['action'] : $_SERVER['REQUEST_URI'];
$target = isset ($args['target'])
? $args['target'] : '_self';
$attributes = isset ($args['attributes'])
? $args['attributes'] : null;
$trackSubmit = isset ($args['trackSubmit'])
? $args['trackSubmit'] : false;
$form = & new HTML_QuickForm ($formName, $method, $action, $target,
$attributes, $trackSubmit);
* Adds DB_Table columns to a pre-existing HTML_QuickForm object.
* @param object &$form An HTML_QuickForm object.
* @param array $cols A sequential array of DB_Table column definitions
* from which to create form elements.
* @param string $arrayName By default, the form will use the names
* of the columns as the names of the form elements. If you pass
* $arrayName, the column names will become keys in an array named
foreach ($cols as $name => $col) {
$elemname = $arrayName . " [$name]";
$form->addGroup ($tmp, $elemname, $col['qf_label']);
* Build a single QuickForm element based on a DB_Table column.
* @param array $col A DB_Table column definition.
* @param string $elemname The name to use for the generated QuickForm
* @return object HTML_QuickForm_Element
if (isset ($col['qf_setvalue'])) {
$setval = $col['qf_setvalue'];
switch ($col['qf_type']) {
$element = & HTML_QuickForm ::createElement (
// WARNING: advcheckbox elements in HTML_QuickForm v3.2.2
// and earlier do not honor setChecked(); they will always
// be un-checked, unless a POST value sets them. Upgrade
if (isset ($setval) && $setval == true ) {
$element->setChecked (true );
$element->setChecked (false );
$col['qf_opts']['format'] = 'Y-m-d';
$element = & HTML_QuickForm ::createElement (
$element->setValue ($setval);
$col['qf_opts']['format'] = 'H:i:s';
$element = & HTML_QuickForm ::createElement (
$element->setValue ($setval);
$col['qf_opts']['format'] = 'Y-m-d H:i:s';
$element = & HTML_QuickForm ::createElement (
$element->setValue ($setval);
$element = & HTML_QuickForm ::createElement (
$element->setValue ($setval);
foreach ($col['qf_vals'] as $btnvalue => $btnlabel) {
if (isset ($setval) && $setval == $btnvalue) {
$col['qf_attrs']['checked'] = 'checked';
$element[] = & HTML_QuickForm ::createElement (
null , // elemname not added because this is a group
$element = & HTML_QuickForm ::createElement (
$element->setSelected ($setval);
if (! isset ($col['qf_attrs']['maxlength']) &&
$col['qf_attrs']['maxlength'] = $col['size'];
$element = & HTML_QuickForm ::createElement (
$element->setValue ($setval);
$element = & HTML_QuickForm ::createElement (
(isset ($setval) ? $setval : '')
* @author Moritz Heidkamp <moritz.heidkamp@invision-team.de>
// not a recognized type. is it registered with QuickForm?
if (HTML_QuickForm ::isTypeRegistered ($col['qf_type'])) {
// yes, create it with some minimalist parameters
$element = & HTML_QuickForm ::createElement (
// set its default value, if there is one
$element->setValue ($setval);
// element type is not registered with QuickForm.
* Build an array of form elements based from DB_Table columns.
* @param array $cols A sequential array of DB_Table column
* definitions from which to create form elements.
* @param string $arrayName By default, the form will use the names
* of the columns as the names of the form elements. If you pass
* $arrayName, the column names will become keys in an array named
* @return array An array of HTML_QuickForm_Element objects.
function &getGroup($cols, $arrayName = null )
foreach ($cols as $name => $col) {
$elemname = $arrayName . " [$name]";
* Adds element rules to a pre-existing HTML_QuickForm object.
* @param object &$form An HTML_QuickForm object.
* @param array $cols A sequential array of DB_Table column definitions
* from which to create form elements.
* @param string $arrayName By default, the form will use the names
* of the columns as the names of the form elements. If you pass
* $arrayName, the column names will become keys in an array named
* @param string $clientValidate By default, validation will match
* the 'qf_client' value from the column definition. However,
* if you set $clientValidate to true or false, this will
* override the value from the column definition.
function addRules(&$form, $cols, $arrayName = null ,
foreach ($cols as $name => $col) {
$elemname = $arrayName . " [$name]";
// make sure all necessary elements are in place
// if clientValidate is specified, override the column
// definition. otherwise use the col def as it is.
// **always** override these rules to make them
// server-side only. suggested by Mark Wiesemann,
// debugged by Hero Wanders.
$onlyServer = array ('filename', 'maxfilesize', 'mimetype',
// loop through the rules and add them
foreach ($col['qf_rules'] as $type => $opts) {
// override the onlyServer types so that we don't attempt
// client-side validation at all.
// $opts is the error message
$form->addRule ($elemname, $opts, $type, null , $validate);
// $opts[0] is the message
// $opts[1] is the size, mimetype, or regex
$form->addRule ($elemname, $opts[0 ], $type, $opts[1 ],
* "Fixes" a DB_Table column definition for QuickForm.
* Makes it so that all the 'qf_*' key constants are populated
* with appropriate default values; also checks the 'require'
* value (if not set, defaults to false).
* @param array &$col A DB_Table column definition.
* @param string $elemname The name for the target form element.
// always have a "require" value, false if not set
if (! isset ($col['require'])) {
// array of acceptable values, typically for
if (! isset ($col['qf_vals'])) {
// are we doing client validation in addition to
// server validation? by default, no.
if (! isset ($col['qf_client'])) {
$col['qf_client'] = false;
// the element type; if not set,
// assigns an element type based on the column type.
// by default, the type is 'text' (unless there are
// values, in which case the type is 'select')
if (! isset ($col['qf_type'])) {
$col['qf_type'] = 'checkbox';
$col['qf_vals'] = array (0 ,1 );
$col['qf_type'] = 'date';
$col['qf_type'] = 'time';
$col['qf_type'] = 'timestamp';
$col['qf_type'] = 'textarea';
if (isset ($col['qf_vals'])) {
$col['qf_type'] = 'select';
$col['qf_type'] = 'text';
// label for the element; defaults to the element
if (! isset ($col['qf_label'])) {
$col['qf_label'] = $elemname . ':';
// special options for the element, typically used
// for 'date' element types
if (! isset ($col['qf_opts'])) {
$col['qf_opts'] = array ();
// array of additional HTML attributes for the element
if (! isset ($col['qf_attrs'])) {
// setting to array() generates an error in HTML_Common
// array of QuickForm validation rules to apply
if (! isset ($col['qf_rules'])) {
$col['qf_rules'] = array ();
// if the element is hidden, then we're done
// (adding rules to hidden elements is mostly useless)
if ($col['qf_type'] == 'hidden') {
// the element is required
if (! isset ($col['qf_rules']['required']) && $col['require']) {
$col['qf_rules']['required'] = sprintf(
$GLOBALS['_DB_TABLE']['qf_rules']['required'],
$numeric = array ('smallint', 'integer', 'bigint', 'decimal',
// the element is numeric
if (! isset ($col['qf_rules']['numeric']) &&
$col['qf_rules']['numeric'] = sprintf(
$GLOBALS['_DB_TABLE']['qf_rules']['numeric'],
// the element has a maximum length
if (! isset ($col['qf_rules']['maxlength']) &&
$GLOBALS['_DB_TABLE']['qf_rules']['maxlength'],
$col['qf_rules']['maxlength'] = array ($msg, $max);
Documentation generated on Mon, 11 Mar 2019 13:52:54 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|