Source for file QuickForm.php
Documentation is available at QuickForm.php
require_once 'HTML/QuickForm.php';
* 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.16 2004/04/20 15:18:30 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 $array_name By default, the form will use the names
* of the columns as the names of the form elements. If you pass
* $array_name, 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
* @return object HTML_QuickForm
function &getForm($cols, $array_name = 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 $array_name By default, the form will use the names
* of the columns as the names of the form elements. If you pass
* $array_name, the column names will become keys in an array named
function addElements(&$form, $cols, $array_name = null )
foreach ($cols as $name => $col) {
$elemname = $array_name . " [$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.
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 (
$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 $array_name By default, the form will use the names
* of the columns as the names of the form elements. If you pass
* $array_name, the column names will become keys in an array named
* @return array An array of HTML_QuickForm_Element objects.
function &getGroup($cols, $array_name = null )
foreach ($cols as $name => $col) {
$elemname = $array_name . " [$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 $array_name By default, the form will use the names
* of the columns as the names of the form elements. If you pass
* $array_name, the column names will become keys in an array named
function addRules(&$form, $cols, $array_name = null )
foreach ($cols as $name => $col) {
$elemname = $array_name . " [$name]";
foreach ($col['qf_rules'] as $type => $opts) {
// $opts is the error message
$form->addRule ($elemname, $opts, $type);
// $opts[0] is the message, $opts[1] is the size 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'])) {
// 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'] = 'select';
if (! isset ($col['qf_vals'])) {
$col['qf_vals'] = array (0 => 'No', 1 => 'Yes');
$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 and not hidden
if (! isset ($col['qf_rules']['required']) &&
$col['qf_rules']['required'] =
'This element is required.';
// the element must be a number
if (! isset ($col['qf_rules']['numeric']) && (
$col['type'] == 'smallint' ||
$col['type'] == 'integer' ||
$col['type'] == 'bigint' ||
$col['type'] == 'decimal'||
$col['type'] == 'single' ||
if (! isset ($col['qf_rules']['numeric'])) {
$col['qf_rules']['numeric'] =
'This element must be numbers only.';
// the element has a maximum length
if (! isset ($col['qf_rules']['maxlength']) &&
$col['qf_rules']['maxlength'] = array (
" This element can be no longer than $max characters." ,
Documentation generated on Mon, 11 Mar 2019 10:15:12 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|