PEAR HTML QuickForm

Dec 01, 2007 22:38

Introduction
The HTML_Quickform package massively reduces the amount of HTML code needed to ouput and validate a form.

Elements

AdvCheckbox

$form->addElement(
'advcheckbox',
string element-name, // name of advcheckbox
string element-label, // label output before advcheckbox
string text, // label output after advcheckbox
mixed attributes, // string or array of attributes
mixed values); // see below
If values is omitted then the value returned is an empty string if not checked, and '1' if checked. If a string value is passed then the return value is empty string/supplied string, and if an array of two strings is passed they are taken as the unchecked/checked values to return.

Button

addElement('button',
string element-name, // name of button
string value, // text of button
mixed attributes); // string or array of attributes

Checkbox

$form->addElement(
'checkbox',
string element-name, // name of checkbox
string element-label, // label output before checkbox
string text, // label output after checkbox
mixed attributes); // string or array of attributes

Date

addElement('date',
string element-name, // name of date
string element-label, // label output before date
array options, // see below
mixed attributes); // string or array of attributes
Let QuickForm worry about multiple date entries, i.e. for setting up a simple credit card check use the following example;

$custom_name = 'Ian Warner';
$cc_account = 'Personal';
$cc_type = 'AMEX';
$cc_expm = '9';
$cc_expy = '2008';

$type_of_account =
array('Office' => 'Office',
'Personal' => 'Personal'
);
$type_of_cards =
array('VISA' => 'VISA',
'MASTERCARD' => 'MASTERCARD',
'AMEX' => 'AMEX'
);

$form->setDefaults(
array( 'custom_name' => $custom_name,
'cc_account' => $cc_account,
'cc_type' => $cc_type,
'CCexpiry' => array('F' => $cc_expm, 'Y' => $cc_expy)));

$form->addElement('text', 'custom_name', 'Custom Name',
array('size' => 50, 'maxlength' => 255));
$form->addElement('select', 'cc_account', 'Type of Account',
$type_of_account);
$form->addElement('select', 'cc_type', 'Type of Card',
$type_of_cards);

$form->addElement('date', 'CCexpiry', 'Credit Card Expiration',
array('format' => 'F-Y',
'minYear' => date('Y'),
'maxYear' => date('Y') + 10));

Hidden

addElement('hidden',
string element-name, // name of hidden element
string value, // value of hidden element
mixed attributes); // string or array of attributes

HierSelect

addElement('hierselect',
string element-name, // name of hierselect element
string label, // text label
mixed attributes); // string or array of attributes

$sel =& $form->addElement('hierselect', 'category',
'Category', null, ' - ');
$mainOptions = array(1 => 'TV Shows', 2 => 'Movies');
$secOptions = array(1 => array(1 => 'Stargate Atlantis'),
2 => array(1 => 'Romance'));
$sel->setOptions(array($mainOptions, $secOptions));

Image

addElement('image',
string element-name, // name for image
string src, // source file of image
mixed attributes); // string or array of attributes

Radio

addElement('radio',
string element-name, // name for radio button
string label, // text to display before button
string text, // text to display after button
int value, // the value returned
mixed attributes); // string or array of attributes

Select

addElement('select',
string element-name, // name of select
string label, // label for select
mixed data, // data for select; see earlier text
mixed attributes); // string or array of attributes

$form->addElement('select', 'type', 'Message Type',
array('SMS'=>'Normal SMS', 'FlashSMS'=>'Flash SMS'));

Static

addElement('static',
string label, // label to display
string text); // text to display

Submit

addElement('submit',
string element-name, // name of submit
string value, // text of button
mixed attributes); // string or array of attributes

$form->addElement('submit', null, 'Submit');

Text

addElement('text',
string element-name, // name of text box
string label, // label
mixed attributes); // string or array of attributes

$form->addElement('text', 'service', 'Title',
array('size'=>30, 'maxlength'=>100));

Textarea

$form->addElement('textarea', 'message', 'Message',
array('cols'=>30, 'rows'=>5));

Rules

$form->addRule('userfile', 'You must select a file',
'required', null, 'client');
Callback rule example to check for duplicate Username:

/**
* Function to check whether the username is already taken.
*
* @param string $username Check username is unique
* @return
*/
function checkUnique($username)
{
// Connect to Database

// Select to see if there is a username already taken

// Return the count

if ($result > 0) {
return false;
} else {
return true;
}
}

$form->registerRule('checkusername', 'callback', 'checkUnique');
$form->addRule('usernmae', 'Username is duplicated please select another', 'checkusername');

Groups
A group allows for elements to be displayed on the same line, and also for rules to be defined for the group

$group[] =& HTML_QuickForm::createElement('text', 'first', 'First');
$group[] =& HTML_QuickForm::createElement('text', 'last', 'Last');
$form->addGroup($group, 'name', 'Name:', ', ');
Group rules as an example only:

$rule['first'][] = array('First is required', 'required');
$rule['first'][] = array('First contains only letters', 'lettersonly');
$rule['last'][] = array('Last is required', 'required');
$rule['last'][] = array('Last contains numbers only', 'regex', '/^\d+$/');
$form->addGroupRule('name', $rule);

Display Form
With Renderer

$form->accept($renderer);
echo $renderer->toHtml();

note, programming

Previous post Next post
Up