sfValidatorBoolean.class.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 10307 2008-07-15 22:19:02Z 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. }