| 1 |
lars |
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: sfValidatorBoolean.class.php 10306 2008-07-15 22:12:35Z Carl.Vondrick $
|
|
|
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 |
}
|