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.
* $Id: builtin-rules.php 288991 2009-09-30 12:34:16Z avb $
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
/* 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/data/quickform.css';
<title>HTML_QuickForm2 basic elements example</title>
// in real application the password check will a bit be different, of course
return ($password == 'qwerty');
require_once 'HTML/QuickForm2.php';
// for file upload to work
$form->setAttribute ('enctype', 'multipart/form-data');
// data source with default values:
'testUsername' => 'luser'
// 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):');
$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');
$username->addRule ('regex', 'Username should contain only letters', '/^[a-zA-Z]+$/');
// old password should be either left blank or be equal to 'qwerty'
$oldPassword->addRule ('empty')
->or_ ($oldPassword->createRule ('callback', 'Wrong password', 'check_password'));
// this behaves exactly as it reads: either "password" and "password repeat"
// are empty or they should be equal, password should be no less than 6 chars
// and old password should be given
$newPassword->addRule ('empty')
->and_ ($repPassword->createRule ('empty'))
->or_ ($repPassword->createRule ('eq', 'The passwords do not match', $newPassword))
->and_ ($newPassword->createRule ('minlength', 'The password is too short', 6 ))
->and_ ($oldPassword->createRule ('nonempty', 'Supply old password if you want to change it'));
// 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
$upload->addRule ('required', 'Please upload picture');
// no longer using 'filename' rule for uploads
$upload->addRule ('regex', 'Allowed extensions: .gif, .jp(e)g, .png', '/\\.(gif|jpe?g|png)$/i');
$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 );
$form->addElement ('submit', 'testSubmit', array ('value' => 'Send'));
$form->toggleFrozen (true );
Documentation generated on Mon, 11 Mar 2019 15:34:58 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|