Development

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

You must first sign up to be able to contribute.

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

Revision 10270, 3.2 kB (checked in by Carl.Vondrick, 6 years ago)

1.1: added deprecated warnings to all sfCompatPlugin classes (closes #3882)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
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  * WARNING: This class is deprecated and will be removed in symfony 1.2.
33  *
34  * @package    symfony
35  * @subpackage validator
36  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
37  * @version    SVN: $Id$
38  * @deprecated Deprecated since symfony 1.1
39  */
40 class sfCompareValidator extends sfValidator
41 {
42   /**
43    * Executes this validator.
44    *
45    * @param mixed A file or parameter value/array
46    * @param error An error message reference
47    *
48    * @return bool true, if this validator executes successfully, otherwise false
49    */
50   public function execute(&$value, &$error)
51   {
52     $check_param    = $this->getParameter('check');
53     $check_operator = $this->getParameter('operator');
54     $check_value    = $this->context->getRequest()->getParameter($check_param);
55
56     switch ($check_operator)
57     {
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         $valid = $value != $check_value;
72         break;
73       case '==':
74       default:
75         $valid = $value == $check_value;
76         break;
77     }
78
79     if (!$valid)
80     {
81       $error = $this->getParameter('compare_error');
82
83       return false;
84     }
85
86     return true;
87   }
88
89   /**
90    * Initializes this validator.
91    *
92    * @param sfContext The current application context
93    * @param array     An associative array of initialization parameters
94    *
95    * @return boolean true, if initialization completes successfully, otherwise false
96    */
97   public function initialize($context, $parameters = null)
98   {
99     // initialize parent
100     parent::initialize($context);
101
102     // set defaults
103     $this->setParameter('operator', '==');
104     $this->setParameter('compare_error', 'Invalid input');
105
106     $this->getParameterHolder()->add($parameters);
107
108     if (!$this->hasParameter('check'))
109     {
110       throw new sfValidatorException('You must specify a "check" parameter for your sfCompareValidator.');
111     }
112
113     if (!in_array($this->getParameter('operator'), array('==', '!=', '>', '<', '<=', '>=')))
114     {
115       throw new sfValidatorException(sprintf('The operator "%s" is not available for your sfCompareValidator.', $this->getParameter('operator')));
116     }
117
118     return true;
119   }
120 }
121
Note: See TracBrowser for help on using the browser.