Source for file builtin-rules.php
Documentation is available at builtin-rules.php
* Usage example for HTML_QuickForm2 package: builtin rules
* The example uses all Rule classes provided with HTML_QuickForm2 and also
* showcases rule chaining.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
/* Set up custom font and form width */
font-family: Arial,sans-serif;
/* Use default styles included with the package */
if ('@data_dir@' != '@' . 'data_dir@') {
$filename = '@data_dir@/HTML_QuickForm2/quickform.css';
.quickform .valid input { background: #F0FFF0; }
.quickform .error input { background: #FFF0F0; }
<script type="text/javascript">
//in real application the password check will a bit be different, of course
function check_password(password)
return (password == 'qwerty');
function enableValidation(box)
box.form.validator = validatorBackup;
validatorBackup = box.form.validator;
box.form.validator = null;
<title>HTML_QuickForm2 built-in rules example</title>
// in real application the password check will a bit be different, of course
return ($password == 'qwerty');
require_once 'HTML/QuickForm2.php';
require_once 'HTML/QuickForm2/Renderer.php';
// for file upload to work
$form->setAttribute ('enctype', 'multipart/form-data');
// data source with default values:
'testUsername' => 'luser',
'friends' => array ('luser123', 'gangsta1998')
// override whatever value was submitted
$form->addElement ('hidden', 'MAX_FILE_SIZE')->setValue ('102400');
// Simple fields validation, rule chaining
$fsAuth = $form->addElement ('fieldset')->setLabel ('Auth credentials');
$username = $fsAuth->addElement ('text', 'testUsername', array ('style' => 'width: 200px;'))
->setLabel ('Username (letters only):');
$email = $fsAuth->addElement ('text', 'testEmail', array ('style' => 'width: 200px'))
$fsPasswords = $fsAuth->addElement ('fieldset')
->setLabel ('Supply password only if you want to change it');
$oldPassword = $fsPasswords->addElement ('password', 'oldPassword', array ('style' => 'width: 200px;'))
->setLabel ('Old password (<i>qwerty</i>):');
$newPassword = $fsPasswords->addElement ('password', 'newPassword', array ('style' => 'width: 200px;'))
->setLabel ('New password (min 6 chars):');
$repPassword = $fsPasswords->addElement ('password', 'newPasswordRepeat', array ('style' => 'width: 200px;'))
->setLabel ('Repeat new password:');
$username->addRule ('required', 'Username is required', null ,
$username->addRule ('regex', 'Username should contain only letters', '/^[a-zA-Z]+$/',
$email->addRule ('email', 'Email address is invalid', null ,
// old password should be either left blank or be equal to 'qwerty'
->or_ ($oldPassword->createRule ('callback', 'Wrong password', 'check_password'));
// this behaves exactly as it reads: either "password" and "password repeat"
// are both empty or they should be equal
->and_ ($repPassword->createRule ('empty'))
->or_ ($repPassword->createRule ('eq', 'The passwords do not match', $newPassword));
// Either new password is not given, or old password is required
->or_ ($oldPassword->createRule ('nonempty', 'Supply old password if you want to change it'));
$newPassword->addRule ('minlength', 'The password is too short', 6 , HTML_QuickForm2_Rule::ONBLUR_CLIENT_SERVER );
// No sense changing the password to the same value
->and_ ($newPassword->createRule ('neq', 'New password is the same as the old one', $oldPassword));
// Grouped elements validation
$fsGrouped = $form->addElement ('fieldset')->setLabel ('Validating grouped elements');
$boxGroup = $fsGrouped->addElement ('group', 'boxes')->setLabel ('Check at least two:');
$boxGroup->addElement ('checkbox', null , array ('value' => 'red'))->setContent ('<span style="color: #f00;">Red</span>');
$boxGroup->addElement ('checkbox', null , array ('value' => 'green'))->setContent ('<span style="color: #0f0;">Green</span>');
$boxGroup->addElement ('checkbox', null , array ('value' => 'blue'))->setContent ('<span style="color: #00f;">Blue</span>');
$boxGroup->addRule ('required', 'Check at least two boxes', 2 ,
$friends = $fsGrouped->addElement ('group', 'friends')->setLabel ('Friends usernames (letters only):')
->setSeparator ('<br />');
$friends->addElement ('text', '0', array ('style' => 'width: 200px;', 'id' => 'friend-1'));
$friends->addElement ('text', '1', array ('style' => 'width: 200px;', 'id' => 'friend-2'));
$friends->addElement ('text', '2', array ('style' => 'width: 200px;', 'id' => 'friend-3'));
$friends->addRule ('each', 'Friends\' usernames should contain only letters',
$friends->createRule ('regex', '', '/^[a-zA-Z]+$/'),
// File uploads validation
$fsUpload = $form->addElement ('fieldset')->setLabel ('Upload picture (try one > 100 kB for fun)');
$upload = $fsUpload->addElement ('file', 'testUpload', array ('style' => 'width: 200px'))
->setLabel ('Picture (gif, jpg, png, <=20kB):');
// no longer using special 'uploadedfile' rule for uploads, allow client-side validation
$upload->addRule ('required', 'Please upload picture', null ,
// no longer using 'filename' rule for uploads, allow client-side validation
$upload->addRule ('regex', 'Allowed extensions: .gif, .jp(e)g, .png', '/\\.(gif|jpe?g|png)$/i',
// these don't work client-side, for obvious reasons
$upload->addRule ('mimetype', 'Your browser doesn\'t think that\'s an image',
array ('image/gif', 'image/png', 'image/jpeg', 'image/pjpeg'));
$upload->addRule ('maxfilesize', 'File is too big, allowed size 20kB', 20480 );
$submitGrp = $form->addElement ('group')->setSeparator (' ');
$submitGrp->addElement ('submit', 'testSubmit', array ('value' => 'Send'));
$submitGrp->addElement ('checkbox', 'clientSide', array ('onclick' => 'enableValidation(this);'))
->setContent ('perform client-side validation')
->setAttribute ('checked'); // override submit value
$form->toggleFrozen (true );
$form->render ($renderer);
// Output javascript libraries, needed for client-side validation
echo $renderer->getJavascriptBuilder ()->getLibraries (true , true );
Documentation generated on Wed, 10 Apr 2019 08:56:08 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|