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 288991 2009-09-30 12:34:16Z avb $
  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. /* Set up custom font and form width */
  17. body {
  18.     margin-left: 10px;
  19.     font-family: Arial,sans-serif;
  20.     font-size: small;
  21. }
  22.  
  23. .quickform {
  24.     min-width: 500px;
  25.     max-width: 600px;
  26.     width: 560px;
  27. }
  28.  
  29. /* Use default styles included with the package */
  30.  
  31. <?php
  32. if ('@data_dir@' != '@' 'data_dir@'{
  33.     $filename '@data_dir@/HTML_QuickForm2/data/quickform.css';
  34. else {
  35.     $filename dirname(dirname(dirname(__FILE__))) '/data/quickform.css';
  36. }
  37. readfile($filename);
  38. ?>
  39.     </style>
  40.     <title>HTML_QuickForm2 basic elements example</title>
  41.   </head>
  42.   <body>
  43. <?php
  44.  
  45. // in real application the password check will a bit be different, of course
  46. function check_password($password)
  47. {
  48.     return ($password == 'qwerty');
  49. }
  50.  
  51. //
  52. // Form setup
  53. //
  54.  
  55. require_once 'HTML/QuickForm2.php';
  56.  
  57. $form = new HTML_QuickForm2('basicRules');
  58. // for file upload to work
  59. $form->setAttribute('enctype''multipart/form-data');
  60.  
  61. // data source with default values:
  62. $form->addDataSource(new HTML_QuickForm2_DataSource_Array(array(
  63.     'testUsername'      => 'luser'
  64. )));
  65.  
  66. // override whatever value was submitted
  67. $form->addElement('hidden''MAX_FILE_SIZE')->setValue('102400');
  68.  
  69. //
  70. // Simple fields validation, rule chaining
  71. //
  72.  
  73. $fsAuth $form->addElement('fieldset')->setLabel('Auth credentials');
  74. $username $fsAuth->addElement('text''testUsername'array('style' => 'width: 200px;'))
  75.                    ->setLabel('Username (letters only):');
  76.  
  77. $fsPasswords $fsAuth->addElement('fieldset')
  78.                       ->setLabel('Supply password only if you want to change it');
  79.  
  80. $oldPassword $fsPasswords->addElement('password''oldPassword'array('style' => 'width: 200px;'))
  81.                            ->setLabel('Old password (<i>qwerty</i>):');
  82. $newPassword $fsPasswords->addElement('password''newPassword'array('style' => 'width: 200px;'))
  83.                            ->setLabel('New password (min 6 chars):');
  84. $repPassword $fsPasswords->addElement('password''newPasswordRepeat'array('style' => 'width: 200px;'))
  85.                            ->setLabel('Repeat new password:');
  86.  
  87. $username->addRule('required''Username is required');
  88. $username->addRule('regex''Username should contain only letters''/^[a-zA-Z]+$/');
  89.  
  90. // old password should be either left blank or be equal to 'qwerty'
  91. $oldPassword->addRule('empty')
  92.             ->or_($oldPassword->createRule('callback''Wrong password''check_password'));
  93.  
  94. // this behaves exactly as it reads: either "password" and "password repeat"
  95. // are empty or they should be equal, password should be no less than 6 chars
  96. // and old password should be given
  97. $newPassword->addRule('empty')
  98.             ->and_($repPassword->createRule('empty'))
  99.             ->or_($repPassword->createRule('eq''The passwords do not match'$newPassword))
  100.             ->and_($newPassword->createRule('minlength''The password is too short'6))
  101.             ->and_($oldPassword->createRule('nonempty''Supply old password if you want to change it'));
  102.  
  103. //
  104. // File uploads validation
  105. //
  106.  
  107. $fsUpload $form->addElement('fieldset')->setLabel('Upload picture (try one &gt; 100 kB for fun)');
  108. $upload $fsUpload->addElement('file''testUpload'array('style' => 'width: 200px'))
  109.                    ->setLabel('Picture (gif, jpg, png, &lt;=20kB):');
  110.  
  111. // no longer using special 'uploadedfile' rule for uploads
  112. $upload->addRule('required''Please upload picture');
  113. // no longer using 'filename' rule for uploads
  114. $upload->addRule('regex''Allowed extensions: .gif, .jp(e)g, .png''/\\.(gif|jpe?g|png)$/i');
  115. $upload->addRule('mimetype''Your browser doesn\'t think that\'s an image',
  116.                  array('image/gif''image/png''image/jpeg''image/pjpeg'));
  117. $upload->addRule('maxfilesize''File is too big, allowed size 20kB'20480);
  118.  
  119. $form->addElement('submit''testSubmit'array('value' => 'Send'));
  120.  
  121. if ($form->validate()) {
  122.     echo "<pre>\n";
  123.     var_dump($form->getValue());
  124.     echo "</pre>\n<hr />";
  125.     $form->toggleFrozen(true);
  126. }
  127.  
  128. echo $form;
  129. ?>
  130.   </body>
  131. </html>

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