public static function advancedValidator($validator, $values) { if ($values['form_type'] === 'input' || $values['form_type'] === 'textarea') { $validator = new sfValidatorInteger(array('required' => false)); $values['value_min'] = $validator->clean($values['value_min']); $values['value_max'] = $validator->clean($values['value_max']); } elseif ($values['form_type'] === 'date') { $validator = new opValidatorDate(array('required' => false)); $validator->clean($values['value_min']); $validator->clean($values['value_max']); } elseif ($values['value_min'] || $values['value_max']) { throw new sfValidatorError($validator, 'invalid'); } return $values; }
public static function validateValue($validator, $values, $valueKey) { $options = array('required' => false); $validator = null; switch ($values['form_type']) { case 'input': case 'textarea': $validator = new sfValidatorInteger($options); break; case 'date': $options['date_format_range_error'] = 'Y-m-d'; $validator = new opValidatorDate($options); break; default: break; // Do nothing. } if (null !== $validator) { try { $cleanValue = $validator->clean($values[$valueKey]); if (!$validator instanceof opValidatorDate) { $values[$valueKey] = $cleanValue; } } catch (Exception $e) { throw new sfValidatorErrorSchema($validator, array($valueKey => new sfValidatorError($validator, 'invalid'))); } } elseif ($values[$valueKey]) { throw new sfValidatorError($validator, 'invalid'); } return $values; }
$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('1989-01-08'), strtotime('1989-01-08'), '->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('1989-01-08 00:00:00'), strtotime('1989-01-08 00:00:00'), '->clean() output format can be change with the date_output option'); $v = new opValidatorDate(); // max and min options $t->diag('max and min options'); $v->setOption('min', '1 Jan 2005'); $v->setOption('max', '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'); } try { $v->clean('18 october 2008'); $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'); } $t->is($v->clean(array('year' => 2006, 'month' => 1, 'day' => 8)), '2006-01-08', '->clean() can accept a max/min option array'); try { $v->clean(array('year' => 1989, 'month' => 1, 'day' => 8));
foreach (array(array('year' => 0, 'month' => 0, 'day' => 0), array('year' => '0', 'month' => '0', 'day' => '0'), array('year' => 0.0, 'month' => 0.0, 'day' => 0.0)) as $input) { try { $v->clean($input); $t->fail('->clean() throws an exception if the date is not empty and required is false'); } catch (sfValidatorError $e) { $t->pass('->clean() throws an exception if the date is not empty and required is false'); $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError'); } } // required = true, isEmpty() = false $t->diag('required = true, isEmpty() = false'); $v = new opValidatorDate(); $v->setOption('empty_value', new DateTime('1989-01-08')); foreach (array(array('year' => '', 'month' => '', 'day' => '', 'hour' => 10), array('year' => null, 'month' => null, 'day' => null, 'hour' => 10), array('year' => false, 'month' => false, 'day' => false, 'hour' => 10), array('year' => array(), 'month' => array(), 'day' => array(), 'hour' => 10), array('year' => new SimpleXMLElement('<hoge />'), 'month' => new SimpleXMLElement('<hoge />'), 'day' => new SimpleXMLElement('<hoge />'), 'hour' => 10)) as $input) { try { $t->is($v->clean($input), '1989-01-08', '->clean() accepts empty valus "Yms" and required is true'); } catch (sfValidatorError $e) { $t->fail('->clean() accepts empty valus "Yms" and required is true'); } } // required = false, isEmpty() = false $t->diag('required = false, isEmpty() = false'); foreach (array(array('year' => 0, 'month' => 0, 'day' => 0, 'hour' => 10), array('year' => '0', 'month' => '0', 'day' => '0', 'hour' => 10), array('year' => 0.0, 'month' => 0.0, 'day' => 0.0, 'hour' => 10)) as $input) { try { $v->clean($input); $t->fail('->clean() accepts not empty valus "Yms" and required is true'); } catch (sfValidatorError $e) { $t->pass('->clean() accepts not empty valus "Yms" and required is true'); $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError'); } }