Development

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

You must first sign up to be able to contribute.

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

Revision 10270, 2.5 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
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  * 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  * WARNING: This class is deprecated and will be removed in symfony 1.2.
27  *
28  * @package    symfony
29  * @subpackage validator
30  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
31  * @version    SVN: $Id$
32  * @deprecated Deprecated since symfony 1.1
33  */
34 class sfCallbackValidator extends sfValidator
35 {
36   /**
37    * Executes this validator.
38    *
39    * @param string A parameter value
40    * @param string An error message reference
41    *
42    * @return boolean true, if this validator executes successfully, otherwise false
43    */
44   public function execute(&$value, &$error)
45   {
46     $callback = $this->getParameterHolder()->get('callback');
47
48     if (!call_user_func($callback, $value))
49     {
50       $error = $this->getParameterHolder()->get('invalid_error');
51
52       return false;
53     }
54
55     return true;
56   }
57
58   /**
59    * Initializes this validator.
60    *
61    * @param sfContext The current application context
62    * @param array   An associative array of initialization parameters
63    *
64    * @return boolean true, if initialization completes successfully, otherwise false
65    */
66   public function initialize($context, $parameters = null)
67   {
68     // initialize parent
69     parent::initialize($context);
70
71     // set defaults
72     $this->getParameterHolder()->set('callback', null);
73     $this->getParameterHolder()->set('invalid_error', 'Invalid input');
74
75     $this->getParameterHolder()->add($parameters);
76
77     // check parameters
78     if (!is_callable($this->getParameterHolder()->get('callback')))
79     {
80       // no pattern specified
81       throw new sfValidatorException('Callback function must be a valid callback using is_callable().');
82     }
83
84     return true;
85   }
86 }
87
Note: See TracBrowser for help on using the browser.