EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
4.01k stars 1.02k forks source link

`DateField` ignores timezone on forms #5576

Open kiler129 opened 1 year ago

kiler129 commented 1 year ago

Describe the bug The DateField seems to completely ignore any timezone settings, which is a problem in some UTC offsets with dates being displayed one day earlier on lists than entered.

image image image image

To Reproduce Running EAB v4.5.0:

# DashboardController
    public function configureCrud(): Crud
    {
        return parent::configureCrud()
                     ->setTimezone('America/Chicago');
    }

# TestCrudController extends \EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController
 public function configureFields(string $pageName): iterable
 {
     yield DateField::new('obtained')
 } 

Additional context In my case Symfony, PHP, and PgSQL are all set to UTC. It seems like EAB correctly formats the date on lists, but doesn't set the correct parameters to Symfony form fields. Ostensibly the fix is simple:

        yield DateField::new('obtained')
            ->setFormTypeOption('model_timezone', 'UTC')
            ->setFormTypeOption('view_timezone', 'America/Chicago')
        ;

However, this looks like it's triggering a Symfony bug as the form component forms data on display (model => form) but not on submission (form => model): image image image

d3pendent commented 3 months ago

I can confirm this behavior. Exactly like @kiler129 described. Is there any workaround? Times are displayed in the correct timezone, but the filter are not calculating with the timezone offset. That leads to wrong results after filtering.

d3pendent commented 3 months ago

I actually just got it to work.

public function configureFilters(Filters $filters): Filters
{
    return parent::configureFilters($filters)
        ->add(
            DateTimeFilter::new('createdAt')
                ->setFormTypeOptions([
                    'value_type_options' => [
                        'model_timezone' => 'UTC',
                        'view_timezone' => 'Europe/Berlin',
                    ],
                ])
        )
    ;
}