Development

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

You must first sign up to be able to contribute.

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

Revision 7902, 2.7 kB (checked in by fabien, 6 years ago)

rename sfValidatorBase to sfValidator in sfCompat10Plugin and renamed the new sfValidator class to sfValidatorBase (closes #3103)

  • 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  * @package    symfony
30  * @subpackage validator
31  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
32  * @author     Sean Kerr <sean@code-box.org>
33  * @version    SVN: $Id$
34  */
35 class sfRegexValidator extends sfValidator
36 {
37   /**
38    * Executes this validator.
39    *
40    * @param string A parameter value
41    * @param string An error message reference
42    *
43    * @return bool true, if this validator executes successfully, otherwise false
44    */
45   public function execute(&$value, &$error)
46   {
47     $match   = $this->getParameterHolder()->get('match');
48     $pattern = $this->getParameterHolder()->get('pattern');
49
50     if (($match && !preg_match($pattern, $value)) ||
51         (!$match && preg_match($pattern, $value)))
52     {
53       $error = $this->getParameterHolder()->get('match_error');
54
55       return false;
56     }
57
58     return true;
59   }
60
61   /**
62    * Initializes this validator.
63    *
64    * @param sfContext The current application context
65    * @param array   An associative array of initialization parameters
66    *
67    * @return bool true, if initialization completes successfully, otherwise false
68    */
69   public function initialize($context, $parameters = null)
70   {
71     // initialize parent
72     parent::initialize($context);
73
74     // set defaults
75     $this->getParameterHolder()->set('match',       true);
76     $this->getParameterHolder()->set('match_error', 'Invalid input');
77     $this->getParameterHolder()->set('pattern',     null);
78
79     $this->getParameterHolder()->add($parameters);
80
81     // check parameters
82     if ($this->getParameterHolder()->get('pattern') == null)
83     {
84       // no pattern specified
85       throw new sfValidatorException('Please specify a PCRE regular expression pattern for your registered RegexValidator.');
86     }
87
88     return true;
89   }
90 }
91
Note: See TracBrowser for help on using the browser.