Development

/branches/1.3/lib/plugins/sfCompat10Plugin/lib/validator/sfCompareValidator.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/plugins/sfCompat10Plugin/lib/validator/sfCompareValidator.class.php

Revision 23810, 3.1 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.3] set svn:eol-style property to native and svn:keywords property to Id on all .php files

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * sfCompareValidator compares two different request parameters.
13  *
14  * <b>Required parameters:</b>
15  *
16  * # <b>check</b>         - [none]          - A request parameter name
17  *
18  * <b>Optional parameters:</b>
19  *
20  * # <b>operator</b>      - [==]            - Comparison operator (available operators: >, >=, <, <=, !==, ==)
21  * # <b>compare_error</b> - [Invalid input] - An error message to use when the two given parameters do not match
22  *
23  * <b>Example:</b>
24  *
25  * passwordValidator:
26  *   class:            sfCompareValidator
27  *   param:
28  *     check:          password2
29  *     operator:       ==
30  *     compare_error:  The passwords you entered do not match.
31  *
32  * @package    symfony
33  * @subpackage validator
34  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
35  * @version    SVN: $Id$
36  */
37 class sfCompareValidator extends sfValidator
38 {
39   /**
40    * Executes this validator.
41    *
42    * @param mixed A file or parameter value/array
43    * @param error An error message reference
44    *
45    * @return bool true, if this validator executes successfully, otherwise false
46    */
47   public function execute(&$value, &$error)
48   {
49     $check_param    = $this->getParameter('check');
50     $check_operator = $this->getParameter('operator');
51     $check_value    = $this->context->getRequest()->getParameter($check_param);
52
53     switch ($check_operator)
54     {
55       case '>':
56         $valid = $value > $check_value;
57         break;
58       case '>=':
59         $valid = $value >= $check_value;
60         break;
61       case '<':
62         $valid = $value < $check_value;
63         break;
64       case '<=':
65         $valid = $value <= $check_value;
66         break;
67       case '!=':
68         $valid = $value != $check_value;
69         break;
70       case '==':
71       default:
72         $valid = $value == $check_value;
73         break;
74     }
75
76     if (!$valid)
77     {
78       $error = $this->getParameter('compare_error');
79
80       return false;
81     }
82
83     return true;
84   }
85
86   /**
87    * Initializes this validator.
88    *
89    * @param sfContext The current application context
90    * @param array     An associative array of initialization parameters
91    *
92    * @return boolean true, if initialization completes successfully, otherwise false
93    */
94   public function initialize($context, $parameters = null)
95   {
96     // initialize parent
97     parent::initialize($context);
98
99     // set defaults
100     $this->setParameter('operator', '==');
101     $this->setParameter('compare_error', 'Invalid input');
102
103     $this->getParameterHolder()->add($parameters);
104
105     if (!$this->hasParameter('check'))
106     {
107       throw new sfValidatorException('You must specify a "check" parameter for your sfCompareValidator.');
108     }
109
110     if (!in_array($this->getParameter('operator'), array('==', '!=', '>', '<', '<=', '>=')))
111     {
112       throw new sfValidatorException(sprintf('The operator "%s" is not available for your sfCompareValidator.', $this->getParameter('operator')));
113     }
114
115     return true;
116   }
117 }
118
Note: See TracBrowser for help on using the browser.