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

  1. <?php
  2. /**
  3.  * Usage example for HTML_QuickForm, using custom validation rules.
  4.  *
  5.  * @author Alexey Borzov <avb@php.net>
  6.  *
  7.  * $Id: rules-custom.php,v 1.2 2003/11/03 20:45:23 avb Exp $ 
  8.  */
  9.  
  10. require_once 'HTML/QuickForm.php';
  11. require_once 'HTML/QuickForm/Rule.php';
  12.  
  13. class RuleNumericRange extends HTML_QuickForm_Rule
  14. {
  15.     function validate($value, $options)
  16.     {
  17.         if (isset($options['min']) && floatval($value) < $options['min']) {
  18.             return false;
  19.         }
  20.         if (isset($options['max']) && floatval($value) > $options['max']) {
  21.             return false;
  22.         }
  23.         return true;
  24.     }
  25.  
  26.     function getValidationScript($options = null)
  27.     {
  28.         $jsCheck = array();
  29.         if (isset($options['min'])) {
  30.             $jsCheck[] = 'Number({jsVar}) >= ' . $options['min'];
  31.         }
  32.         if (isset($options['max'])) {
  33.             $jsCheck[] = 'Number({jsVar}) <= ' . $options['max'];
  34.         }
  35.         return array('', "{jsVar} != '' && !(" . implode(' && ', $jsCheck) . ')');
  36.     } // end func getValidationScript
  37. }
  38.  
  39. // In case you are wondering, this checks whether there are too many
  40. // CAPITAL LETTERS in the string
  41. function countUpper($value, $limit = null)
  42. {
  43.     if (empty($value)) {
  44.         return false;
  45.     }
  46.     if (!isset($limit)) {
  47.         $limit = 0.5;
  48.     }
  49.     $upper = array_filter(preg_split('//', $value, -1, PREG_SPLIT_NO_EMPTY), 'ctype_upper');
  50.     return (count($upper) / strlen($value)) <= $limit;
  51. }
  52.  
  53. // BC thingie: it expects the first param to be element name
  54. function countUpper_old($name, $value, $limit = null)
  55. {
  56.     if (empty($value)) {
  57.         return false;
  58.     }
  59.     if (!isset($limit)) {
  60.         $limit = 0.5;
  61.     }
  62.     $upper = array_filter(preg_split('//', $value, -1, PREG_SPLIT_NO_EMPTY), 'ctype_upper');
  63.     return (count($upper) / strlen($value)) <= $limit;
  64. }
  65.  
  66. $form =& new HTML_QuickForm('custom');
  67.  
  68. $form->addElement('header', null, 'Custom rule class');
  69.  
  70. // registering the custom rule class
  71. $form->registerRule('numRange', null, 'RuleNumericRange');
  72. $form->addElement('text', 'rNumber_1_10', 'The number (1-10):');
  73. $form->addRule('rNumber_1_10', 'Enter number from 1 to 10', 'numRange', array('min' => 1, 'max' => 10), 'client');
  74.  
  75. // adding an instance of the custom rule class without registering
  76. $form->addElement('text', 'rNonnegative', 'Nonnegative number:');
  77. $form->addRule('rNonnegative', 'Enter nonnegative number', new RuleNumericRange(), array('min' => 0), 'client');
  78.  
  79. // adding a classname of the custom rule class without registering
  80. $form->addElement('text', 'rNonpositive', 'Nonpositive number:');
  81. $form->addRule('rNonpositive', 'Enter nonpositive number', 'RuleNumericRange', array('max' => 0), 'client');
  82.  
  83. $form->addElement('header', null, 'Using callbacks');
  84.  
  85. // using callback without registering
  86. $form->addElement('text', 'rUpper_0_5', 'Some (preferrably lowercase) text:');
  87. $form->addRule('rUpper_0_5', 'There are too many CAPITAL LETTERS', 'callback', 'countUpper');
  88.  
  89. // register with 'callback' type
  90. $form->registerRule('upper', 'callback', 'countUpper');
  91. $form->addElement('text', 'rUpper_0_25', 'Some (mostly lowercase) text:');
  92. $form->addRule('rUpper_0_25', 'There are too many CAPITAL LETTERS', 'upper', 0.25);
  93.  
  94. // BC feature: register with 'function' type
  95. $form->registerRule('upperOld', 'function', 'countUpper_old');
  96. $form->addElement('text', 'rUpper_0', 'Some lowercase text:');
  97. $form->addRule('rUpper_0', 'There are CAPITAL LETTERS, this is not allowed', 'upperOld', 0);
  98.  
  99. $form->addElement('submit', null, 'Send');
  100.  
  101. $form->applyFilter(array('rUpper_0_5', 'rUpper_0_25', 'rUpper_0'), 'trim');
  102. $form->applyFilter(array('rNumber_1_10', 'rNonnegative', 'rNonpositive'), 'floatval');
  103.  
  104. $form->validate();
  105.  
  106. $form->display();
  107. ?>
  108.