| 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 |
* sfValidatorDateRange validates a range of date. It also converts the input values to valid dates.
|
|
|
13 |
*
|
|
|
14 |
* @package symfony
|
|
|
15 |
* @subpackage validator
|
|
|
16 |
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
17 |
* @version SVN: $Id: sfValidatorDateRange.class.php 32810 2011-07-21 05:18:56Z fabien $
|
|
|
18 |
*/
|
|
|
19 |
class sfValidatorDateRange extends sfValidatorBase
|
|
|
20 |
{
|
|
|
21 |
/**
|
|
|
22 |
* Configures the current validator.
|
|
|
23 |
*
|
|
|
24 |
* Available options:
|
|
|
25 |
*
|
|
|
26 |
* * from_date: The from date validator (required)
|
|
|
27 |
* * to_date: The to date validator (required)
|
|
|
28 |
* * from_field: The name of the "from" date field (optional, default: from)
|
|
|
29 |
* * to_field: The name of the "to" date field (optional, default: to)
|
|
|
30 |
*
|
|
|
31 |
* @param array $options An array of options
|
|
|
32 |
* @param array $messages An array of error messages
|
|
|
33 |
*
|
|
|
34 |
* @see sfValidatorBase
|
|
|
35 |
*/
|
|
|
36 |
protected function configure($options = array(), $messages = array())
|
|
|
37 |
{
|
|
|
38 |
$this->setMessage('invalid', 'The begin date must be before the end date.');
|
|
|
39 |
|
|
|
40 |
$this->addRequiredOption('from_date');
|
|
|
41 |
$this->addRequiredOption('to_date');
|
|
|
42 |
$this->addOption('from_field', 'from');
|
|
|
43 |
$this->addOption('to_field', 'to');
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* @see sfValidatorBase
|
|
|
48 |
*/
|
|
|
49 |
protected function doClean($value)
|
|
|
50 |
{
|
|
|
51 |
$fromField = $this->getOption('from_field');
|
|
|
52 |
$toField = $this->getOption('to_field');
|
|
|
53 |
|
|
|
54 |
$value[$fromField] = $this->getOption('from_date')->clean(isset($value[$fromField]) ? $value[$fromField] : null);
|
|
|
55 |
$value[$toField] = $this->getOption('to_date')->clean(isset($value[$toField]) ? $value[$toField] : null);
|
|
|
56 |
|
|
|
57 |
if ($value[$fromField] && $value[$toField])
|
|
|
58 |
{
|
|
|
59 |
$v = new sfValidatorSchemaCompare($fromField, sfValidatorSchemaCompare::LESS_THAN_EQUAL, $toField, array('throw_global_error' => true), array('invalid' => $this->getMessage('invalid')));
|
|
|
60 |
$v->clean($value);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
return $value;
|
|
|
64 |
}
|
|
|
65 |
}
|