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.  * $Id: builtin-rules.php,v 1.1 2007/10/15 08:28:52 avb Exp $
  9.  */ 
  10. ?>
  11. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  12.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  13. <html>
  14.   <head>
  15.     <style type="text/css">
  16. /* styles borrowed from an older release of Tableless Renderer for QF. 
  17.    Newer styles work worse with nested fieldsets */
  18. body {
  19.     margin-left: 10px;
  20.     font-family: Arial,sans-serif;
  21.     font-size: small;
  22. }
  23. form {
  24.     margin: 0;
  25.     padding: 0;
  26.     min-width: 500px;
  27.     max-width: 600px;
  28.     width: 560px;
  29. }
  30. form fieldset {
  31.     border: 1px solid black;
  32.     padding: 10px 5px;
  33.     margin: 0;
  34.     /*width: 560px;*/
  35. }
  36. form fieldset.hidden {
  37.     border: 0;
  38. }
  39. form fieldset legend {
  40.     font-weight: bold;
  41. }
  42. form label {
  43.     margin: 0 0 0 5px;
  44. }
  45. form label.qflabel {
  46.     display: block;
  47.     float: left;
  48.     width: 200px;
  49.     padding: 0;
  50.     margin: 5px 0 0 0;
  51.     text-align: right;
  52. }
  53. form input, form textarea, form select {
  54.     width: auto;
  55. }
  56. form textarea {
  57.     overflow: auto;
  58. }
  59. form br {
  60.     clear: left;
  61. }
  62. form div.qfelement {
  63.     display: inline;
  64.     float: left;
  65.     margin: 5px 0 0 10px;
  66.     padding: 0;
  67. }
  68. form div.qfreqnote {
  69.     font-size: 80%; 
  70. }
  71. form span.error, form span.required {
  72.     color: red;
  73. }
  74. form div.error {
  75.     border: 1px solid red;
  76.     padding: 5px;
  77. }
  78.     </style>
  79.     <title>HTML_QuickForm2 basic elements example</title>
  80.   </head>
  81.   <body>
  82. <?php
  83.  
  84. //
  85. // Helper functions
  86. //
  87.  
  88. function output_element($element)
  89. {
  90.     if ('fieldset' == $element->getType()) {
  91.         output_fieldset($element);
  92.     elseif ('hidden' == $element->getType()) {
  93.         echo '<div style="display: none;">' $element->__toString("</div>\n";
  94.     else {
  95.         $required $element->isRequired();
  96.         $error    $element->getError();
  97.         echo '<div class="qfrow"><label class="qflabel" for="' $element->getId(.
  98.              '">' ($required'<span class="required">*</span>'''$element->getLabel(
  99.              '</label> <div class="qfelement' (strlen($error)' error''''">' .
  100.              (strlen($error)'<span class="error">' $error '</span><br />'''.
  101.              $element->__toString("</div></div><br />\n";
  102.     }
  103. }
  104.  
  105. function output_fieldset($fieldset)
  106. {
  107.     echo '<fieldset' $fieldset->getAttributes(true">\n<legend>" .
  108.          $fieldset->getLabel("</legend>\n";
  109.     foreach ($fieldset as $element{
  110.         output_element($element);
  111.     }
  112.     echo "</fieldset>\n";
  113. }
  114.  
  115. // in real application the password check will a bit be different, of course
  116. function check_password($password)
  117. {
  118.     return ($password == 'qwerty');
  119. }
  120.  
  121. //
  122. // Form setup
  123. //
  124.  
  125. require_once 'HTML/QuickForm2.php';
  126.  
  127. $form = new HTML_QuickForm2('basicRules');
  128. // for file upload to work
  129. $form->setAttribute('enctype''multipart/form-data');
  130.  
  131. // data source with default values:
  132. $form->addDataSource(new HTML_QuickForm2_DataSource_Array(array(
  133.     'testUsername'      => 'luser'
  134. )));
  135.  
  136. // override whatever value was submitted
  137. $form->addElement('hidden''MAX_FILE_SIZE')->setValue('102400');
  138.  
  139. //
  140. // Simple fields validation, rule chaining
  141. //
  142.  
  143. $fsAuth $form->addElement('fieldset')->setLabel('Auth credentials');
  144. $username $fsAuth->addElement('text''testUsername'array('style' => 'width: 200px;'))
  145.                    ->setLabel('Username (letters only):');
  146.  
  147. $fsPasswords $fsAuth->addElement('fieldset')
  148.                       ->setLabel('Supply password only if you want to change it');
  149.  
  150. $oldPassword $fsPasswords->addElement('password''oldPassword'array('style' => 'width: 200px;'))
  151.                            ->setLabel('Old password (<i>qwerty</i>):');
  152. $newPassword $fsPasswords->addElement('password''newPassword'array('style' => 'width: 200px;'))
  153.                            ->setLabel('New password (min 6 chars):');
  154. $repPassword $fsPasswords->addElement('password''newPasswordRepeat'array('style' => 'width: 200px;'))
  155.                            ->setLabel('Repeat new password:');
  156.  
  157. $username->addRule('required''Username is required');
  158. $username->addRule('regex''Username should contain only letters''/^[a-zA-Z]+$/');
  159.  
  160. // old password should be either left blank or be equal to 'qwerty'
  161. $oldPassword->addRule('empty')
  162.             ->or_($oldPassword->createRule('callback''Wrong password''check_password'));
  163.  
  164. // this behaves exactly as it reads: either "password" and "password repeat"
  165. // are empty or they should be equal, password should be no less than 6 chars
  166. // and old password shuld be given
  167. $newPassword->addRule('empty')
  168.             ->and_($repPassword->addRule('empty'))
  169.             ->or_($newPassword->createRule('eq''The passwords do not match'$repPassword))
  170.             ->and_($newPassword->createRule('minlength''The password is too short'6))
  171.             ->and_($oldPassword->createRule('nonempty''Supply old password if you want to change it'));
  172.  
  173. //
  174. // File uploads validation
  175. //
  176.  
  177. $fsUpload $form->addElement('fieldset')->setLabel('Upload picture (try one &gt; 100 kB for fun)');
  178. $upload $fsUpload->addElement('file''testUpload'array('style' => 'width: 200px'))
  179.                    ->setLabel('Picture (gif, jpg, png, &lt;=20kB):');
  180.  
  181. // no longer using special 'uploadedfile' rule for uploads
  182. $upload->addRule('required''Please upload picture');
  183. // no longer using 'filename' rule for uploads
  184. $upload->addRule('regex''Allowed extensions: .gif, .jp(e)g, .png''/\\.(gif|jpe?g|png)$/i');
  185. $upload->addRule('mimetype''Your browser doesn\'t think that\'s an image',
  186.                  array('image/gif''image/png''image/jpeg''image/pjpeg'));
  187. $upload->addRule('maxfilesize''File is too big, allowed size 20kB'20480);
  188.  
  189. $form->addElement('submit''testSubmit'array('value' => 'Send'));
  190.  
  191. if ($form->validate()) {
  192.     echo "<pre>\n";
  193.     var_dump($form->getValue());
  194.     echo "</pre>\n<hr />";
  195.     $form->toggleFrozen(true);
  196. }
  197.  
  198. echo '<form' $form->getAttributes(true">\n";
  199. foreach ($form as $element{
  200.     output_element($element);
  201. }
  202.  
  203. ?>
  204. </form>
  205.   </body>
  206. </html>

Documentation generated on Mon, 11 Mar 2019 15:10:21 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.