home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / rules-builtin.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  4.8 KB  |  88 lines

  1. <?php
  2. /**
  3.  * Usage example for HTML_QuickForm, built-in validation rules.
  4.  *
  5.  * @author Alexey Borzov <avb@php.net>
  6.  *
  7.  * $Id: rules-builtin.php,v 1.3 2004/03/02 21:14:41 avb Exp $ 
  8.  */
  9.  
  10. require_once 'HTML/QuickForm.php';
  11.  
  12. $form =& new HTML_QuickForm('builtin');
  13.  
  14. // We need an additional label below the element
  15. $renderer =& $form->defaultRenderer();
  16. $renderer->setElementTemplate(<<<EOT
  17. <tr>
  18.     <td align="right" valign="top" nowrap="nowrap"><!-- BEGIN required --><span style="color: #ff0000">*</span><!-- END required --><b>{label}</b></td>
  19.     <td valign="top" align="left">
  20.         <!-- BEGIN error --><span style="color: #ff0000">{error}</span><br /><!-- END error -->{element}
  21.         <!-- BEGIN label_2 --><br/><span style="font-size: 80%">{label_2}</span><!-- END label_2 -->
  22.     </td>
  23. </tr>
  24.  
  25. EOT
  26. );
  27.  
  28. $form->addElement('header', null, 'Required rule');
  29. $form->addElement('text', 'rRequired', array('Required:', 'Rule type \'required\'<br />Note: when the field is not \'required\' and is empty, other validation rules will <b>not</b> be applied to it'));
  30. $form->addRule('rRequired', 'The field is required', 'required', null, 'client');
  31.  
  32. // RangeLength rules
  33. $form->addElement('header', null, 'Range based rules');
  34. $form->addElement('text', 'rMaxLength', array('Maximum length check (5):', 'Rule type \'maxlength\', $format = 5'));
  35. $form->addElement('text', 'rMinLength', array('Minimum length check (5):', 'Rule type \'minlength\', $format = 5'));
  36. $form->addElement('text', 'rRangeLength', array('Length range check (5-10):', 'Rule type \'rangelength\', $format = array(5, 10)'));
  37.  
  38. $form->addRule('rMaxLength', 'Should be less than or equal to 5 symbols', 'maxlength', 5, 'client');
  39. $form->addRule('rMinLength', 'Should be more than or equal to 5 symbols', 'minlength', 5, 'client');
  40. $form->addRule('rRangeLength', 'Should be between 5 and 10 symbols', 'rangelength', array(5,10), 'client');
  41.  
  42. // Email rule
  43. $form->addElement('header', null, 'Email rule');
  44. $form->addElement('text', 'rEmail', array('Email check:', 'Rule type \'email\''));
  45. $form->addRule('rEmail', 'Should contain a valid email', 'email', null, 'client');
  46.  
  47. // RegEx rules
  48. $form->addElement('header', null, 'Regex based rules');
  49. $form->addElement('text', 'rRegex', array('Letters \'A\', \'B\', \'C\' only:', 'Rule type \'regex\' with $format = \'/^[ABCabc]+$/\''));
  50. $form->addElement('text', 'rLettersOnly', array('Letters only:', 'Rule type \'lettersonly\''));
  51. $form->addElement('text', 'rAlphaNumeric', array('Alphanumeric:', 'Rule type \'alphanumeric\''));
  52. $form->addElement('text', 'rNumeric', array('Numeric:', 'Rule type \'numeric\''));
  53. $form->addElement('text', 'rNoPunctuation', array('No punctuation:', 'Rule type \'nopunctuation\''));
  54. $form->addElement('text', 'rNonZero', array('Nonzero:', 'Rule type \'nonzero\''));
  55.  
  56. $form->addRule('rRegex', 'Should contain letters A, B, C only', 'regex', '/^[ABCabc]+$/', 'client');
  57. $form->addRule('rLettersOnly', 'Should contain letters only', 'lettersonly', null, 'client');
  58. $form->addRule('rAlphaNumeric', 'Should be alphanumeric', 'alphanumeric', null, 'client');
  59. $form->addRule('rNumeric', 'Should be numeric', 'numeric', null, 'client');
  60. $form->addRule('rNoPunctuation', 'Should contain no punctuation', 'nopunctuation', null, 'client');
  61. $form->addRule('rNonZero', 'Should be nonzero', 'nonzero', null, 'client');
  62.  
  63. // Compare rule
  64. $form->addElement('header', null, 'Compare rule');
  65. $form->addElement('password', 'cmpPasswd', 'Password:');
  66. $form->addElement('password', 'cmpRepeat', array('Repeat password:', 'Rule type \'compare\', added to array(\'cmpPasswd\', \'cmpRepeat\')'));
  67. $form->addRule(array('cmpPasswd', 'cmpRepeat'), 'The passwords do not match', 'compare', null, 'client');
  68.  
  69. // File rules
  70. $form->addElement('header', null, 'Uploaded file rules');
  71. $form->addElement('file', 'tstUpload', array('Upload file:', 'Rule types: \'uploadedfile\', \'maxfilesize\' with $format = 10240, \'mimetype\' with $format = \'text/xml\', filename with $format = \'/\\.xml$/\'<br />Validation for files is obviuosly <b>server-side only</b>'));
  72. $form->addRule('tstUpload', 'Upload is required', 'uploadedfile');
  73. $form->addRule('tstUpload', 'File size should be less than 10kb', 'maxfilesize', 10240);
  74. $form->addRule('tstUpload', 'File type should be text/xml', 'mimetype', 'text/xml');
  75. $form->addRule('tstUpload', 'File name should be *.xml', 'filename', '/\\.xml$/');
  76.  
  77. $form->addElement('header', null, 'Submit the form');
  78. $submit[] =& $form->createElement('submit', null, 'Send');
  79. $submit[] =& $form->createElement('checkbox', 'clientSide', null, 'use client-side validation', array('checked' => 'checked', 'onclick' => "if (this.checked) {this.form.onsubmit = oldHandler;} else {oldHandler = this.form.onsubmit; this.form.onsubmit = null;}"));
  80. $form->addGroup($submit, null, null, ' ', false);
  81.  
  82. $form->applyFilter('__ALL__', 'trim');
  83.  
  84. $form->validate();
  85.  
  86. $form->display();
  87. ?>
  88.