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

Delegate filter query by empty value to filters #6402

Merged

Conversation

peter-gribanov
Copy link
Contributor

Subject

If the filter value is empty, but the filtering type has been changed from the default value, then this is an indication that the user wants to search by empty value.
At this level, we cannot determine whether a search can be performed on an empty value and therefore delegate it to filters.

Part of fix for #3569.

Changelog

### Changed
- Delegate filter query by empty value to filters.

@VincentLanglet
Copy link
Member

If the filter value is empty, but the filtering type has been changed from the default value, then this is an indication that the user wants to search by empty value.

I understand your point, that's not a bad idea.
I'm just afraid that some users could by tricked by this, if they were erasing the filter value to disable the filter.
It's even more tricky since we can hide the filtering type AFTER having modified it.

ping @sonata-project/contributors

src/Datagrid/Datagrid.php Outdated Show resolved Hide resolved
@peter-gribanov peter-gribanov force-pushed the filter_by_empty_value branch 3 times, most recently from 1e38aa1 to 20e6b90 Compare September 23, 2020 09:55
VincentLanglet
VincentLanglet previously approved these changes Sep 23, 2020
{
if (isset($this->values['_per_page'])) {
if (isset($this->values['_per_page']['value'])) {
return (int) $this->values['_per_page']['value'];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that this data comes from a request and it contains a string with a number, not a integer. Therefore, i am doing explicit type casting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's never too safe

@VincentLanglet VincentLanglet requested a review from a team September 27, 2020 15:33
@SonataCI
Copy link
Collaborator

Could you please rebase your PR and fix merge conflicts?

@VincentLanglet
Copy link
Member

VincentLanglet commented Nov 4, 2020

An upgrade note seems nice to explain the (big) impact of this change.
If the user doesn't check for empty value in his filter, it will be applied.

### Empty values in datagrid filters

Now empty values are passed to datagrid filters. If you have custom datagrid filters, add empty string check to them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VincentLanglet please review

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding an exemple could be helpful:

Something like

In the following exemple

->add('with_open_comments', CallbackFilter::class, [
    'callback' => static function(ProxyQueryInterface $queryBuilder, string $alias, string $field, array $value): bool {
        if (!$value['value']) {
            return false;
        }

        $queryBuilder
            ->leftJoin(sprintf('%s.comments', $alias), 'c')
            ->andWhere('c.status = :status')
            ->setParameter('status', Comment::STATUS_MODERATE);

         return true;
    },
    'field_type' => CheckboxType::class
 ]);

The !$value['value'] check is required to not filtering by '' if you didn't used the filter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add this

@VincentLanglet
Copy link
Member

You requested changes, can you take a new look @core23 ? =)


private function applySorting(): void
{
if (isset($this->values['_sort_by'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you invert this check to fail fast

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this?


private function getMaxPerPage(int $default): int
{
if (isset($this->values['_per_page'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here


private function getPage(int $default): int
{
if (isset($this->values['_page'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here

VincentLanglet
VincentLanglet previously approved these changes Nov 10, 2020
core23
core23 previously approved these changes Nov 10, 2020
UPGRADE-3.x.md Outdated Show resolved Hide resolved
UPGRADE-3.x.md Outdated Show resolved Hide resolved
UPGRADE-3.x.md Outdated Show resolved Hide resolved
UPGRADE-3.x.md Outdated Show resolved Hide resolved
extract code from Datagrid::buildPager() into separate methods to improve readability

add upgrade info

add example

invert check to fail fast

fix text in UPGRADE
@VincentLanglet VincentLanglet merged commit bb4b462 into sonata-project:3.x Nov 10, 2020
@VincentLanglet
Copy link
Member

Thanks ! @peter-gribanov

@peter-gribanov peter-gribanov deleted the filter_by_empty_value branch November 11, 2020 05:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants