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

Source for file rules-builtin.php

Documentation is available at rules-builtin.php

  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.4 2004/11/26 10:24:54 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 obviously <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($submitnullnull'&nbsp;'false);
  81.  
  82. $form->applyFilter('__ALL__''trim');
  83.  
  84. $form->validate();
  85.  
  86. $form->display();
  87. ?>

Documentation generated on Mon, 11 Mar 2019 14:16:36 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.