Development

/branches/1.2/lib/validator/sfValidatorBoolean.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/validator/sfValidatorBoolean.class.php

Revision 10306, 1.5 kB (checked in by Carl.Vondrick, 6 years ago)

1.2: fixed handling boolean values in radio and checkbox widgets (closes #3825, #3996, #4002)

  • 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) 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  * sfValidatorBoolean validates a boolean. It also converts the input value to a valid boolean.
13  *
14  * @package    symfony
15  * @subpackage validator
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfValidatorBoolean extends sfValidatorBase
20 {
21   /**
22    * Configures the current validator.
23    *
24    * Available options:
25    *
26    *  * true_values:  The list of true values
27    *  * false_values: The list of false values
28    *
29    * @param array $options    An array of options
30    * @param array $messages   An array of error messages
31    *
32    * @see sfValidatorBase
33    */
34   protected function configure($options = array(), $messages = array())
35   {
36     $this->addOption('true_values', array('true', 't', 'yes', 'y', 'on', '1'));
37     $this->addOption('false_values', array('false', 'f', 'no', 'n', 'off', '0'));
38
39     $this->setOption('required', false);
40     $this->setOption('empty_value', false);
41   }
42
43   /**
44    * @see sfValidatorBase
45    */
46   protected function doClean($value)
47   {
48     if (in_array($value, $this->getOption('true_values')))
49     {
50       return true;
51     }
52
53     if (in_array($value, $this->getOption('false_values')))
54     {
55       return false;
56     }
57
58     throw new sfValidatorError($this, 'invalid', array('value' => $value));
59   }
60 }
61
Note: See TracBrowser for help on using the browser.