Development

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

You must first sign up to be able to contribute.

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

Revision 10270, 2.8 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  * (c) 2004-2006 Sean Kerr <sean@code-box.org>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * sfRegexValidator allows you to match a value against a regular expression
14  * pattern.
15  *
16  * <b>Required parameters:</b>
17  *
18  * # <b>pattern</b> - [none] - A PCRE, preg_match() style regular expression
19  *                             pattern.
20  *
21  * <b>Optional parameters:</b>
22  *
23  * # <b>match</b>       - [true]          - Indicates that the pattern must be
24  *                                          matched or must not match.
25  * # <b>match_error</b> - [Invalid input] - An error message to use when the
26  *                                          input does not meet the regex
27  *                                          specifications.
28  *
29  * WARNING: This class is deprecated and will be removed in symfony 1.2.
30  *
31  * @package    symfony
32  * @subpackage validator
33  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
34  * @author     Sean Kerr <sean@code-box.org>
35  * @version    SVN: $Id$
36  * @deprecated Deprecated since symfony 1.1
37  */
38 class sfRegexValidator extends sfValidator
39 {
40   /**
41    * Executes this validator.
42    *
43    * @param string A parameter value
44    * @param string An error message reference
45    *
46    * @return bool true, if this validator executes successfully, otherwise false
47    */
48   public function execute(&$value, &$error)
49   {
50     $match   = $this->getParameterHolder()->get('match');
51     $pattern = $this->getParameterHolder()->get('pattern');
52
53     if (($match && !preg_match($pattern, $value)) ||
54         (!$match && preg_match($pattern, $value)))
55     {
56       $error = $this->getParameterHolder()->get('match_error');
57
58       return false;
59     }
60
61     return true;
62   }
63
64   /**
65    * Initializes this validator.
66    *
67    * @param sfContext The current application context
68    * @param array   An associative array of initialization parameters
69    *
70    * @return bool true, if initialization completes successfully, otherwise false
71    */
72   public function initialize($context, $parameters = null)
73   {
74     // initialize parent
75     parent::initialize($context);
76
77     // set defaults
78     $this->getParameterHolder()->set('match',       true);
79     $this->getParameterHolder()->set('match_error', 'Invalid input');
80     $this->getParameterHolder()->set('pattern',     null);
81
82     $this->getParameterHolder()->add($parameters);
83
84     // check parameters
85     if ($this->getParameterHolder()->get('pattern') == null)
86     {
87       // no pattern specified
88       throw new sfValidatorException('Please specify a PCRE regular expression pattern for your registered RegexValidator.');
89     }
90
91     return true;
92   }
93 }
94
Note: See TracBrowser for help on using the browser.