add support for expanded values on TimeField

This commit is contained in:
Thomas Portelange 2024-04-08 17:08:00 +02:00 committed by GitHub
parent 39c9e0d31c
commit c0fc61be1e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 12 additions and 2 deletions

View File

@ -388,8 +388,8 @@ class TimeField extends TextField
* Convert frontend time to the internal representation (ISO 8601).
* The frontend time is also in ISO 8601 when $html5=true.
*
* @param string $time
* @return string The formatted time, or null if not a valid time
* @param ?string $time
* @return ?string The formatted time, or null if not a valid time
*/
protected function frontendToInternal($time)
{
@ -400,6 +400,16 @@ class TimeField extends TextField
$toFormatter = $this->getInternalFormatter();
$timestamp = $fromFormatter->parse($time);
// Retry with expanded value
if ($timestamp === false) {
$zeroFormat = $fromFormatter->format(strtotime(date('Y-01-01 00:00:00')));
$expectedLength = strlen($zeroFormat);
if (strlen($time) < $expectedLength) {
$expandedValue = $time . substr($zeroFormat, strlen($time));
$timestamp = $fromFormatter->parse($expandedValue);
}
}
// 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()) {