HTML_QuickForm2
[ class tree: HTML_QuickForm2 ] [ index: HTML_QuickForm2 ] [ all elements ]

Source for file builtin-rules.php

Documentation is available at builtin-rules.php

  1. <?php
  2. /**
  3.  * Usage example for HTML_QuickForm2 package: builtin rules
  4.  *
  5.  * The example uses all Rule classes provided with HTML_QuickForm2 and also
  6.  * showcases rule chaining.
  7.  */
  8. ?>
  9. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  10.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  11. <html xmlns="http://www.w3.org/1999/xhtml">
  12.   <head>
  13.     <style type="text/css">
  14. /* Set up custom font and form width */
  15. body {
  16.     margin-left: 10px;
  17.     font-family: Arial,sans-serif;
  18.     font-size: small;
  19. }
  20.  
  21. .quickform {
  22.     min-width: 500px;
  23.     max-width: 600px;
  24.     width: 560px;
  25. }
  26.  
  27. /* Use default styles included with the package */
  28.  
  29. <?php
  30. if ('@data_dir@' != '@' 'data_dir@'{
  31.     $filename '@data_dir@/HTML_QuickForm2/quickform.css';
  32. else {
  33.     $filename dirname(dirname(dirname(__FILE__))) '/data/quickform.css';
  34. }
  35. readfile($filename);
  36. ?>
  37.  
  38. .quickform .valid input { background: #F0FFF0; }
  39. .quickform .error input { background: #FFF0F0; }
  40.  
  41.     </style>
  42.     <script type="text/javascript">
  43. // <![CDATA[
  44. var validatorBackup;
  45.  
  46. //in real application the password check will a bit be different, of course
  47. function check_password(password)
  48. {
  49.     return (password == 'qwerty');
  50. }
  51.  
  52. function enableValidation(box)
  53. {
  54.     if (box.checked) {
  55.         box.form.validator = validatorBackup;
  56.     } else {
  57.         validatorBackup = box.form.validator;
  58.         box.form.validator = null;
  59.     }
  60. }
  61.  
  62. // ]]>
  63.     </script>
  64.     <title>HTML_QuickForm2 built-in rules example</title>
  65.   </head>
  66.   <body>
  67. <?php
  68.  
  69. // in real application the password check will a bit be different, of course
  70. function check_password($password)
  71. {
  72.     return ($password == 'qwerty');
  73. }
  74.  
  75. //
  76. // Form setup
  77. //
  78.  
  79. require_once 'HTML/QuickForm2.php';
  80. require_once 'HTML/QuickForm2/Renderer.php';
  81.  
  82. $form = new HTML_QuickForm2('basicRules');
  83. // for file upload to work
  84. $form->setAttribute('enctype''multipart/form-data');
  85.  
  86. // data source with default values:
  87. $form->addDataSource(new HTML_QuickForm2_DataSource_Array(array(
  88.     'testUsername'      => 'luser',
  89.     'friends'           => array('luser123''gangsta1998')
  90. )));
  91.  
  92. // override whatever value was submitted
  93. $form->addElement('hidden''MAX_FILE_SIZE')->setValue('102400');
  94.  
  95. //
  96. // Simple fields validation, rule chaining
  97. //
  98.  
  99. $fsAuth $form->addElement('fieldset')->setLabel('Auth credentials');
  100. $username $fsAuth->addElement('text''testUsername'array('style' => 'width: 200px;'))
  101.                    ->setLabel('Username (letters only):');
  102. $email $fsAuth->addElement('text''testEmail'array('style' => 'width: 200px'))
  103.                 ->setLabel('Email:');
  104.  
  105. $fsPasswords $fsAuth->addElement('fieldset')
  106.                       ->setLabel('Supply password only if you want to change it');
  107.  
  108. $oldPassword $fsPasswords->addElement('password''oldPassword'array('style' => 'width: 200px;'))
  109.                            ->setLabel('Old password (<i>qwerty</i>):');
  110. $newPassword $fsPasswords->addElement('password''newPassword'array('style' => 'width: 200px;'))
  111.                            ->setLabel('New password (min 6 chars):');
  112. $repPassword $fsPasswords->addElement('password''newPasswordRepeat'array('style' => 'width: 200px;'))
  113.                            ->setLabel('Repeat new password:');
  114.  
  115. $username->addRule('required''Username is required'null,
  116.                    HTML_QuickForm2_Rule::ONBLUR_CLIENT_SERVER);
  117. $username->addRule('regex''Username should contain only letters''/^[a-zA-Z]+$/',
  118.                    HTML_QuickForm2_Rule::ONBLUR_CLIENT_SERVER);
  119. $email->addRule('email''Email address is invalid'null,
  120.                 HTML_QuickForm2_Rule::ONBLUR_CLIENT_SERVER);
  121.  
  122. // old password should be either left blank or be equal to 'qwerty'
  123. $oldPassword->addRule('empty'''nullHTML_QuickForm2_Rule::ONBLUR_CLIENT_SERVER)
  124.             ->or_($oldPassword->createRule('callback''Wrong password''check_password'));
  125.  
  126. // this behaves exactly as it reads: either "password" and "password repeat"
  127. // are both empty or they should be equal
  128. $newPassword->addRule('empty'''nullHTML_QuickForm2_Rule::ONBLUR_CLIENT_SERVER)
  129.             ->and_($repPassword->createRule('empty'))
  130.             ->or_($repPassword->createRule('eq''The passwords do not match'$newPassword));
  131.  
  132. // Either new password is not given, or old password is required
  133. $newPassword->addRule('empty'''nullHTML_QuickForm2_Rule::ONBLUR_CLIENT_SERVER)
  134.             ->or_($oldPassword->createRule('nonempty''Supply old password if you want to change it'));
  135.  
  136. $newPassword->addRule('minlength''The password is too short'6HTML_QuickForm2_Rule::ONBLUR_CLIENT_SERVER);
  137.  
  138. // No sense changing the password to the same value
  139. $newPassword->addRule('nonempty'''nullHTML_QuickForm2_Rule::ONBLUR_CLIENT_SERVER)
  140.             ->and_($newPassword->createRule('neq''New password is the same as the old one'$oldPassword));
  141.  
  142. //
  143. // Grouped elements validation
  144. //
  145.  
  146. $fsGrouped $form->addElement('fieldset')->setLabel('Validating grouped elements');
  147. $boxGroup $fsGrouped->addElement('group''boxes')->setLabel('Check at least two:');
  148. $boxGroup->addElement('checkbox'nullarray('value' => 'red'))->setContent('<span style="color: #f00;">Red</span>');
  149. $boxGroup->addElement('checkbox'nullarray('value' => 'green'))->setContent('<span style="color: #0f0;">Green</span>');
  150. $boxGroup->addElement('checkbox'nullarray('value' => 'blue'))->setContent('<span style="color: #00f;">Blue</span>');
  151.  
  152. $boxGroup->addRule('required''Check at least two boxes'2,
  153.                    HTML_QuickForm2_Rule::ONBLUR_CLIENT_SERVER);
  154.  
  155. $friends $fsGrouped->addElement('group''friends')->setLabel('Friends usernames (letters only):')
  156.                      ->setSeparator('<br />');
  157. $friends->addElement('text''0'array('style' => 'width: 200px;''id' => 'friend-1'));
  158. $friends->addElement('text''1'array('style' => 'width: 200px;''id' => 'friend-2'));
  159. $friends->addElement('text''2'array('style' => 'width: 200px;''id' => 'friend-3'));
  160.  
  161. $friends->addRule('each''Friends\' usernames should contain only letters',
  162.                   $friends->createRule('regex''''/^[a-zA-Z]+$/'),
  163.                   HTML_QuickForm2_Rule::ONBLUR_CLIENT_SERVER);
  164.  
  165. //
  166. // File uploads validation
  167. //
  168.  
  169. $fsUpload $form->addElement('fieldset')->setLabel('Upload picture (try one &gt; 100 kB for fun)');
  170. $upload $fsUpload->addElement('file''testUpload'array('style' => 'width: 200px'))
  171.                    ->setLabel('Picture (gif, jpg, png, &lt;=20kB):');
  172.  
  173. // no longer using special 'uploadedfile' rule for uploads, allow client-side validation
  174. $upload->addRule('required''Please upload picture'null,
  175.                  HTML_QuickForm2_Rule::ONBLUR_CLIENT_SERVER);
  176. // no longer using 'filename' rule for uploads, allow client-side validation
  177. $upload->addRule('regex''Allowed extensions: .gif, .jp(e)g, .png''/\\.(gif|jpe?g|png)$/i',
  178.                  HTML_QuickForm2_Rule::ONBLUR_CLIENT_SERVER);
  179.  
  180. // these don't work client-side, for obvious reasons
  181. $upload->addRule('mimetype''Your browser doesn\'t think that\'s an image',
  182.                  array('image/gif''image/png''image/jpeg''image/pjpeg'));
  183. $upload->addRule('maxfilesize''File is too big, allowed size 20kB'20480);
  184.  
  185. $submitGrp $form->addElement('group')->setSeparator('&nbsp;&nbsp;&nbsp;');
  186.  
  187. $submitGrp->addElement('submit''testSubmit'array('value' => 'Send'));
  188. $submitGrp->addElement('checkbox''clientSide'array('onclick' => 'enableValidation(this);'))
  189.           ->setContent('perform client-side validation')
  190.           ->setAttribute('checked')// override submit value
  191.  
  192. if ($form->validate()) {
  193.     echo "<pre>\n";
  194.     var_dump($form->getValue());
  195.     echo "</pre>\n<hr />";
  196.     $form->toggleFrozen(true);
  197. }
  198.  
  199. $renderer HTML_QuickForm2_Renderer::factory('default');
  200. $form->render($renderer);
  201. // Output javascript libraries, needed for client-side validation
  202. echo $renderer->getJavascriptBuilder()->getLibraries(truetrue);
  203. echo $renderer;
  204. ?>
  205.   </body>
  206. </html>

Documentation generated on Wed, 10 Apr 2019 08:56:08 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.