sfValidatorBoolean.class.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfValidatorBoolean validates a boolean. It also converts the input value to a valid boolean.
  11. *
  12. * @package symfony
  13. * @subpackage validator
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfValidatorBoolean.class.php 10306 2008-07-15 22:12:35Z Carl.Vondrick $
  16. */
  17. class sfValidatorBoolean extends sfValidatorBase
  18. {
  19. /**
  20. * Configures the current validator.
  21. *
  22. * Available options:
  23. *
  24. * * true_values: The list of true values
  25. * * false_values: The list of false values
  26. *
  27. * @param array $options An array of options
  28. * @param array $messages An array of error messages
  29. *
  30. * @see sfValidatorBase
  31. */
  32. protected function configure($options = array(), $messages = array())
  33. {
  34. $this->addOption('true_values', array('true', 't', 'yes', 'y', 'on', '1'));
  35. $this->addOption('false_values', array('false', 'f', 'no', 'n', 'off', '0'));
  36. $this->setOption('required', false);
  37. $this->setOption('empty_value', false);
  38. }
  39. /**
  40. * @see sfValidatorBase
  41. */
  42. protected function doClean($value)
  43. {
  44. if (in_array($value, $this->getOption('true_values')))
  45. {
  46. return true;
  47. }
  48. if (in_array($value, $this->getOption('false_values')))
  49. {
  50. return false;
  51. }
  52. throw new sfValidatorError($this, 'invalid', array('value' => $value));
  53. }
  54. }