Better second handling

This commit is contained in:
Ingo Schommer 2017-04-26 13:49:29 +12:00
parent d2132e85db
commit e97783b057
3 changed files with 13 additions and 1 deletions

View File

@ -182,6 +182,14 @@ class DatetimeField extends TextField
$fromFormatter = $this->getFormatter();
$toFormatter = $this->getISO8601Formatter();
$timestamp = $fromFormatter->parse($datetime);
// Try to parse time without seconds, since that's a valid HTML5 submission format
// See https://html.spec.whatwg.org/multipage/infrastructure.html#times
if ($timestamp === false && $this->getHTML5()) {
$fromFormatter->setPattern(str_replace(':ss', '', $fromFormatter->getPattern()));
$timestamp = $fromFormatter->parse($datetime);
}
if ($timestamp === false) {
return null;
}

View File

@ -389,7 +389,7 @@ class TimeField extends TextField
// Try to parse time without seconds, since that's a valid HTML5 submission format
// See https://html.spec.whatwg.org/multipage/infrastructure.html#times
if ($timestamp === false && $this->getHTML5()) {
$fromFormatter->setPattern('HH:mm');
$fromFormatter->setPattern(str_replace(':ss', '', DBTime::ISO_TIME));
$timestamp = $fromFormatter->parse($time);
}

View File

@ -136,6 +136,10 @@ class DatetimeFieldTest extends SapphireTest
$f = new DatetimeField('Datetime', 'Datetime', '2003-03-29 00:00:00');
$this->assertTrue($f->validate(new RequiredFields()));
$f = (new DatetimeField('Datetime', 'Datetime'))
->setSubmittedValue('2003-03-29 00:00');
$this->assertTrue($f->validate(new RequiredFields()), 'Leaving out seconds (like many browsers)');
$f = new DatetimeField('Datetime', 'Datetime', 'wrong');
$this->assertFalse($f->validate(new RequiredFields()));
}