public function validateOpenDate($validator, $value)
 {
     if ($this->isNew()) {
         $dateValidator = new sfValidatorDate(array('min' => strtotime(date('Y-m-d'))), array('min' => sfContext::getInstance()->getI18N()->__('The open date must be after now.')));
         $value['open_date'] = $dateValidator->clean($value['open_date']);
     }
     return $value;
 }
 public function validateOpenDate($validator, $value)
 {
     if ($this->isNew()) {
         $dateValidator = new sfValidatorDate(array('min' => strtotime(date('Y-m-d')), 'required' => false), array('min' => 'The open date must be after now.'));
         try {
             $value['open_date'] = $dateValidator->clean($value['open_date']);
         } catch (sfValidatorError $e) {
             throw new sfValidatorErrorSchema($validator, array('open_date' => $e));
         }
     }
     return $value;
 }
 protected function checkRange($range = array())
 {
     // checking single values
     if (isset($range['date_from'])) {
         $v = new sfValidatorDate();
         $range['date_from'] = $v->clean($range['date_from']);
     }
     if (isset($range['date_to'])) {
         $v = new sfValidatorDate();
         $range['date_to'] = $v->clean($range['date_to']);
     }
     if (isset($range['kilometers_from'])) {
         $v = new sfValidatorInteger(array('min' => 0));
         $range['kilometers_from'] = $v->clean($range['kilometers_from']);
     }
     if (isset($range['kilometers_to'])) {
         $v = new sfValidatorInteger(array('min' => 0));
         $range['kilometers_to'] = $v->clean($range['kilometers_to']);
     }
     // checking ranges
     if (isset($range['date_from']) && isset($range['kilometers_from'])) {
         throw new sfException('Only one between "date_from" and "kilometers_from" can be set.');
     }
     if (isset($range['date_to']) && isset($range['kilometers_to'])) {
         throw new sfException('Only one between "date_to" and "kilometers_to" can be set.');
     }
     $this->log('Range: ');
     if (isset($range['date_from'])) {
         $this->log('  - From ' . $range['date_from']);
     }
     if (isset($range['kilometers_from'])) {
         $this->log('  - From ' . $range['kilometers_from'] . ' km');
     }
     if (isset($range['date_to'])) {
         $this->log('  - To ' . $range['date_to']);
     }
     if (isset($range['kilometers_to'])) {
         $this->log('  - To ' . $range['kilometers_to'] . ' km');
     }
     if (!isset($range['date_from']) && !isset($range['kilometers_from'])) {
         $range['kilometers_from'] = 0;
         $this->log('  - From 0 km');
     }
     if (!isset($range['date_to']) && !isset($range['kilometers_to'])) {
         $range['date_to'] = date('Y-m-d');
         $this->log('  - To ' . date('Y-m-d'));
     }
     $this->log(' ');
     return $range;
 }
$v->setOption('date_format', '~(?P<day>\\d{2})/(?P<month>\\d{2})/(?P<year>\\d{4}) (?P<hour>\\d{2})\\:(?P<minute>\\d{2})~');
$t->is($v->clean('18/10/2005 12:30'), '2005-10-18 12:30:00', '->clean() can accept date time with the with_time option');
$v->setOption('date_format', null);
// change date output
$t->diag('change date output');
$v->setOption('with_time', false);
$v->setOption('date_output', 'U');
$t->is($v->clean(time()), time(), '->clean() output format can be change with the date_output option');
$v->setOption('datetime_output', 'U');
$v->setOption('with_time', true);
$t->is($v->clean(time()), time(), '->clean() output format can be change with the date_output option');
// required
$v = new sfValidatorDate();
foreach (array(array('year' => '', 'month' => '', 'day' => ''), array('year' => null, 'month' => null, 'day' => null), '', null) as $input) {
    try {
        $v->clean($input);
        $t->fail('->clean() throws an exception if the date is empty and required is true');
    } catch (sfValidatorError $e) {
        $t->pass('->clean() throws an exception if the date is empty and required is true');
    }
}
// max and min options
$t->diag('max and min options');
$v->setOption('min', strtotime('1 Jan 2005'));
$v->setOption('max', strtotime('31 Dec 2007'));
$t->is($v->clean('18 october 2005'), '2005-10-18', '->clean() can accept a max/min option');
try {
    $v->clean('18 october 2004');
    $t->fail('->clean() throws an exception if the date is not within the range provided by the min/max options');
} catch (sfValidatorError $e) {
    $t->pass('->clean() throws an exception if the date is not within the range provided by the min/max options');
Example #5
0
$v->setOption('date_format', '~(?P<day>\\d{2})/(?P<month>\\d{2})/(?P<year>\\d{4}) (?P<hour>\\d{2})\\:(?P<minute>\\d{2})~');
$t->is($v->clean('18/10/2005 12:30'), '2005-10-18 12:30:00', '->clean() can accept date time with the with_time option');
$v->setOption('date_format', null);
// change date output
$t->diag('change date output');
$v->setOption('with_time', false);
$v->setOption('date_output', 'U');
$t->is($v->clean(time()), time(), '->clean() output format can be change with the date_output option');
$v->setOption('datetime_output', 'U');
$v->setOption('with_time', true);
$t->is($v->clean(time()), time(), '->clean() output format can be change with the date_output option');
// required
$v = new sfValidatorDate();
foreach (array(array('year' => '', 'month' => '', 'day' => ''), array('year' => null, 'month' => null, 'day' => null), '', null) as $input) {
    try {
        $v->clean($input);
        $t->fail('->clean() throws an exception if the date is empty and required is true');
    } catch (sfValidatorError $e) {
        $t->pass('->clean() throws an exception if the date is empty and required is true');
    }
}
// max and min options
$t->diag('max and min options');
$v->setOption('min', strtotime('1 Jan 2005'));
$v->setOption('max', strtotime('31 Dec 2007'));
$t->is($v->clean('18 october 2005'), '2005-10-18', '->clean() can accept a max/min option');
try {
    $v->clean('18 october 2004');
    $t->fail('->clean() throws an exception if the date is not within the range provided by the min/max options');
} catch (sfValidatorError $e) {
    $t->pass('->clean() throws an exception if the date is not within the range provided by the min/max options');
$v->setOption('date_format', '~(?P<day>\\d{2})/(?P<month>\\d{2})/(?P<year>\\d{4}) (?P<hour>\\d{2})\\:(?P<minute>\\d{2})~');
$t->is($v->clean('18/10/2005 12:30'), '2005-10-18 12:30:00', '->clean() can accept date time with the with_time option');
$v->setOption('date_format', null);
// change date output
$t->diag('change date output');
$v->setOption('with_time', false);
$v->setOption('date_output', 'U');
$t->is($v->clean(time()), time(), '->clean() output format can be change with the date_output option');
$v->setOption('datetime_output', 'U');
$v->setOption('with_time', true);
$t->is($v->clean(time()), time(), '->clean() output format can be change with the date_output option');
// required
$v = new sfValidatorDate();
foreach (array(array('year' => '', 'month' => '', 'day' => ''), array('year' => null, 'month' => null, 'day' => null), '', null) as $input) {
    try {
        $v->clean($input);
        $t->fail('->clean() throws an exception if the date is empty and required is true');
    } catch (sfValidatorError $e) {
        $t->pass('->clean() throws an exception if the date is empty and required is true');
    }
}
// max and min options
$t->diag('max and min options');
$v->setOption('min', strtotime('1 Jan 2005'));
$v->setOption('max', strtotime('31 Dec 2007'));
$t->is($v->clean('18 october 2005'), '2005-10-18', '->clean() can accept a max/min option');
try {
    $v->clean('18 october 2004');
    $t->fail('->clean() throws an exception if the date is not within the range provided by the min/max options');
} catch (sfValidatorError $e) {
    $t->pass('->clean() throws an exception if the date is not within the range provided by the min/max options');