sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

Filter are not submitted when they are all equal to the defaults values. #7250

Closed VincentLanglet closed 3 years ago

VincentLanglet commented 3 years ago

I found one use-case not fixed by https://github.com/sonata-project/SonataAdminBundle/pull/6871

Let's say you have two filter A and B. Let's say the default situation is "The list is filtered by filter A with the value "TRUE"".

If I change the filter A to the value "FALSE", and submit, it works fine. But then, if I change to the value "TRUE", and submit, the "FALSE" is still used. If I change again to the value "TRUE", and I also set a value for the filter B, and submit, now the "TRUE" value is used.

I think that if all the values submitted are equal to the default value, the filter is not (or not correctly) submitted so no value are changed. Maybe we should be sure to always keep one field submitted (?).

cc @kirya-dev and @willemverspyck who works on this recently.

VincentLanglet commented 3 years ago

The issue seems to be related to the getFilterParameters behavior:

if (false !== $this->persistFilters && $this->hasFilterPersister()) {
    // if reset filters is asked, remove from storage
    if ('reset' === $this->getRequest()->query->get('filters')) {
        $this->getFilterPersister()->reset($this->getCode());
    }

    // if no filters, fetch from storage
    // otherwise save to storage
    if (empty($filters)) {
        $filters = $this->getFilterPersister()->get($this->getCode());
    } else {
        $this->getFilterPersister()->set($this->getCode(), $filters);
    }
}

If all the value submitted are removed, the $filters are coming from the FilterPersisterInterface, so the old value are used. If at least one (non-default) value is set, the filter persister is correctly updated.

One solution would be to