[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/validator/ -> sfRegexValidator.class.php (source)

   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.
   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 <skerr@mojavi.org>
  33   * @version    SVN: $Id: sfRegexValidator.class.php 3233 2007-01-11 21:01:08Z fabien $
  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        $error = 'Please specify a PCRE regular expression pattern for your registered RegexValidator';
  86  
  87        throw new sfValidatorException($error);
  88      }
  89  
  90      return true;
  91    }
  92  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7