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

Source for file rules-custom.php

Documentation is available at rules-custom.php

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

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