Relax time input format requirements (#5504)

* remove locale support from time-input, only allow 12 and 24 hour format
* added blur listeners to parse all kinds of time inputs
* fix duration inputs like ":9" for 9 minutes
This commit is contained in:
Kevin Papst
2025-05-28 09:36:29 +02:00
committed by GitHub
parent e9c172daea
commit 81107377f4
9 changed files with 361 additions and 17 deletions

View File

@@ -162,6 +162,7 @@ final class RegenerateLocalesCommand extends Command
$settings['time'] = str_replace("\u{202f}", ' ', $settings['time']);
// keep it simple, we don't need to convert it during runtime
// ISO-8601 defines that 24-hour format should always use a leading zero: use HH instead of H
$settings['time'] = str_replace('HH', 'H', $settings['time']);
$settings['time'] = str_replace('H', 'HH', $settings['time']);

View File

@@ -11,6 +11,7 @@ namespace App\Form\Type;
use App\Configuration\LocaleService;
use App\Utils\FormFormatConverter;
use App\Utils\JavascriptFormatConverter;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Exception\TransformationFailedException;
@@ -18,33 +19,47 @@ use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
final class TimePickerType extends AbstractType
{
public function __construct(private LocaleService $localeService)
public function __construct(private readonly LocaleService $localeService)
{
}
public function configureOptions(OptionsResolver $resolver): void
{
$format = $this->localeService->getTimeFormat(\Locale::getDefault());
$converter = new FormFormatConverter();
$formFormat = $converter->convert($format);
$resolver->setDefaults([
'input' => 'string',
'format' => $formFormat,
'placeholder' => $formFormat, // $format
'locale' => \Locale::getDefault(),
'model_timezone' => date_default_timezone_get(),
'view_timezone' => date_default_timezone_get(),
'block_prefix' => 'time'
]);
$resolver->setDefault('time_format', function (Options $options): string {
// We used the configured time format via "getTimeFormat()" for entering times before, but it caused issues.
// So now we only allow two different input types: 12-hour with AM/PM suffix and 24-hour
return $this->localeService->is24Hour($options['locale']) ? 'HH:mm' : 'h:mm a';
});
$resolver->setDefault('format', function (Options $options): string {
$converter = new FormFormatConverter();
return $converter->convert($options['time_format']);
});
$resolver->setDefault('placeholder', function (Options $options): string {
return $options['time_format'];
});
}
public function buildView(FormView $view, FormInterface $form, array $options): void
{
$view->vars['format'] = $options['format'];
$view->vars['time_format'] = $options['time_format'];
$view->vars['js_format'] = (new JavascriptFormatConverter())->convert($options['time_format']); // @phpstan-ignore argument.type
}
public function buildForm(FormBuilderInterface $builder, array $options): void