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

Source for file Compare.php

Documentation is available at Compare.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Alexey Borzov <avb@php.net>                                  |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Compare.php,v 1.3 2003/11/03 16:08:24 avb Exp $
  20.  
  21. require_once 'HTML/QuickForm/Rule.php';
  22.  
  23. /**
  24.  * Rule to compare two form fields
  25.  * 
  26.  * The most common usage for this is to ensure that the password
  27.  * confirmation field matches the password field
  28.  * 
  29.  * @access public
  30.  * @package HTML_QuickForm
  31.  * @version $Revision: 1.3 $
  32.  */
  33. {
  34.    /**
  35.     * Possible operators to use
  36.     * @var array 
  37.     * @access private
  38.     */
  39.     var $_operators = array(
  40.         'eq'  => '==',
  41.         'neq' => '!=',
  42.         'gt'  => '>',
  43.         'gte' => '>=',
  44.         'lt'  => '<',
  45.         'lte' => '<='
  46.     );
  47.  
  48.  
  49.    /**
  50.     * Returns the operator to use for comparing the values
  51.     * 
  52.     * @access private
  53.     * @param  string     operator name
  54.     * @return string     operator to use for validation
  55.     */
  56.     function _findOperator($name)
  57.     {
  58.         if (empty($name)) {
  59.             return '==';
  60.         elseif (isset($this->_operators[$name])) {
  61.             return $this->_operators[$name];
  62.         elseif (in_array($name$this->_operators)) {
  63.             return $name;
  64.         else {
  65.             return '==';
  66.         }
  67.     }
  68.  
  69.  
  70.     function validate($values$operator = null)
  71.     {
  72.         $operator $this->_findOperator($operator);
  73.         if ('==' != $operator && '!=' != $operator{
  74.             $compareFn create_function('$a, $b''return floatval($a) ' $operator ' floatval($b);');
  75.         else {
  76.             $compareFn create_function('$a, $b''return $a ' $operator ' $b;');
  77.         }
  78.         
  79.         return $compareFn($values[0]$values[1]);
  80.     }
  81.  
  82.  
  83.     function getValidationScript($operator = null)
  84.     {
  85.         $operator $this->_findOperator($operator);
  86.         if ('==' != $operator && '!=' != $operator{
  87.             $check = "!(Number({jsVar}[0]) {$operator} Number({jsVar}[1]))";
  88.         else {
  89.             $check = "!({jsVar}[0] {$operator} {jsVar}[1])";
  90.         }
  91.         return array(''"'' != {jsVar}[0] && {$check}");
  92.     }
  93. }
  94. ?>

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