silverstripe / silverstripe-framework

Silverstripe Framework, the MVC framework that powers Silverstripe CMS
https://www.silverstripe.org
BSD 3-Clause "New" or "Revised" License
723 stars 821 forks source link

ENH add support for expanded values on Time/DateField #11193

Closed lekoala closed 1 month ago

lekoala commented 5 months ago

Description

Currently, partial values in date/time field will results in null values. This is a bit annoying for time value (input 18:--:-- will be null) but is even more obvious on datetime field (inputing the date without the time results in null)

Manual testing steps

Only input partial values, it still submit properly

Issues

Pull request checklist

lekoala commented 5 months ago

Still work in progress

the basic idea would be to add some kind of common helper

    private function expandValue(?string $value, IntlDateFormatter $formatter): int|false
    {
        $zeroFormat = $formatter->format(strtotime(date('Y-01-01 00:00:00')));
        $expectedLength = strlen($zeroFormat);
        if (strlen($value) >= $expectedLength) {
            return false;
        }
        $expandedValue = $value . substr($zeroFormat, strlen($value));
        return $formatter->parse($expandedValue);
    }

i realize that there are many cases where you can setValue, setSubmittedValue, convert from frontend to internal, tidy internals, ... it's not always super consistent and basically i'd like partial values to work everywhere without too much issues

GuySartorelli commented 4 months ago

@lekoala Are you still keen to work on this? It seems like it'd be a really useful feature.

GuySartorelli commented 1 month ago

I'm going to close this for now as there hasn't been any action for a few months. Feel free to open a new PR once this is ready for review.