Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimeField should deal better with partial times #11172

Open
4 tasks done
lekoala opened this issue Mar 6, 2024 · 1 comment
Open
4 tasks done

TimeField should deal better with partial times #11172

lekoala opened this issue Mar 6, 2024 · 1 comment

Comments

@lekoala
Copy link
Contributor

lekoala commented Mar 6, 2024

Description

Not technically a bug, but still annoying...

When using a timefield not in html5, when you enter a partial value (10:00) it will fail because it doesn't match the format (hh:mm:ss)... but that's also kind of too strict

I would suggest to expand value as needed (expand 10:00 to 10:00:00) if the input is smaller than the pattern

Additional context or points of discussion

Here is an example of what I did in my own class. Side benefit : no need to have a distinct handling of html5 vs non html5 inputs, they are all treated equal.

    /**
     * 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
     */
    protected function frontendToInternal($time)
    {
        if (!$time) {
            return null;
        }
        $fromFormatter = $this->getFrontendFormatter();
        $toFormatter = $this->getInternalFormatter();

        // Expand time as needed by increment of 3 using :00
        $patternLength = strlen($fromFormatter->getPattern());
        while (strlen($time) < $patternLength) {
            $time .= ":00";
        }

        $timestamp = $fromFormatter->parse($time);

        // If timestamp still can't be detected, we've got an invalid time
        if ($timestamp === false) {
            return null;
        }

        return $toFormatter->format($timestamp);
    }

Validations

  • You intend to implement the feature yourself
  • You have read the contributing guide
  • You strongly believe this feature should be in core, rather than being its own community module
  • You have checked for existing issues or pull requests related to this feature (and didn't find any)

PRs

@GuySartorelli
Copy link
Member

Sounds like a great idea! I'd definitely welcome a PR that implements this, provided it's BC safe of course.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants