sfValidatorDateRange.class.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. * sfValidatorDateRange validates a range of date. It also converts the input values to valid dates.
  11. *
  12. * @package symfony
  13. * @subpackage validator
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfValidatorDateRange.class.php 11671 2008-09-19 14:07:21Z fabien $
  16. */
  17. class sfValidatorDateRange extends sfValidatorBase
  18. {
  19. /**
  20. * Configures the current validator.
  21. *
  22. * Available options:
  23. *
  24. * * from_date: The from date validator (required)
  25. * * to_date: The to date validator (required)
  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->addMessage('invalid', 'The begin date must be before the end date.');
  35. $this->addRequiredOption('from_date');
  36. $this->addRequiredOption('to_date');
  37. }
  38. /**
  39. * @see sfValidatorBase
  40. */
  41. protected function doClean($value)
  42. {
  43. $value['from'] = $this->getOption('from_date')->clean(isset($value['from']) ? $value['from'] : null);
  44. $value['to'] = $this->getOption('to_date')->clean(isset($value['to']) ? $value['to'] : null);
  45. if ($value['from'] && $value['to'])
  46. {
  47. $v = new sfValidatorSchemaCompare('from', sfValidatorSchemaCompare::LESS_THAN_EQUAL, 'to', array('throw_global_error' => true), array('invalid' => $this->getMessage('invalid')));
  48. $v->clean($value);
  49. }
  50. return $value;
  51. }
  52. }