Exemplo n.º 1
0
$t->is($v->clean('18/10/2005'), '2005-10-18 00:00:00', '->clean() can accept date time with the with_time option');
$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('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');
Exemplo n.º 2
0
}
// required = false, isEmpty() = false
$t->diag('required = false, isEmpty() = false');
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');