[ Index ]
 

Code source de Symfony 1.0.0

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

title

Body

[fermer]

/lib/validator/ -> sfCallbackValidator.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   *
   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   * sfCallbackValidator allows you to use a custom callback function or method to
  13   * validate the input. The function should return true on valid and false on invalid
  14   * and should be callable using is_callable().
  15   *
  16   * <b>Required parameters:</b>
  17   *
  18   * # <b>callback</b> - [none] - A valid callback function or Class::method array.
  19   * When using class/method specify it as an array in yml file as [class, method]
  20   *
  21   * <b>Optional parameters:</b>
  22   *
  23   * # <b>invalid_error</b> - [Invalid input] - An error message to use when the
  24   *                                          input fails the callback check
  25   *
  26   * @package    symfony
  27   * @subpackage validator
  28   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  29   * @version    SVN: $Id: sfCallbackValidator.class.php 3329 2007-01-23 08:29:34Z fabien $
  30   */
  31  class sfCallbackValidator extends sfValidator
  32  {
  33    /**
  34     * Executes this validator.
  35     *
  36     * @param string A parameter value
  37     * @param string An error message reference
  38     *
  39     * @return boolean true, if this validator executes successfully, otherwise false
  40     */
  41    public function execute(&$value, &$error)
  42    {
  43      $callback = $this->getParameterHolder()->get('callback');
  44  
  45      if (!call_user_func($callback, $value))
  46      {
  47        $error = $this->getParameterHolder()->get('invalid_error');
  48  
  49        return false;
  50      }
  51  
  52      return true;
  53    }
  54  
  55    /**
  56     * Initializes this validator.
  57     *
  58     * @param sfContext The current application context
  59     * @param array   An associative array of initialization parameters
  60     *
  61     * @return boolean true, if initialization completes successfully, otherwise false
  62     */
  63    public function initialize($context, $parameters = null)
  64    {
  65      // initialize parent
  66      parent::initialize($context);
  67  
  68      // set defaults
  69      $this->getParameterHolder()->set('callback', null);
  70      $this->getParameterHolder()->set('invalid_error', 'Invalid input');
  71  
  72      $this->getParameterHolder()->add($parameters);
  73  
  74      // check parameters
  75      if (!is_callable($this->getParameterHolder()->get('callback')))
  76      {
  77        // no pattern specified
  78        $error = 'Callback function must be a valid callback using is_callable()';
  79  
  80        throw new sfValidatorException($error);
  81      }
  82  
  83      return true;
  84    }
  85  }


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