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

ExistsFilterType default choice #304

Open
umpirsky opened this issue May 23, 2023 · 4 comments
Open

ExistsFilterType default choice #304

umpirsky opened this issue May 23, 2023 · 4 comments
Labels
DX Issues and PRs aimed at improving Developer eXperience. Enhancement Minor issues and PRs improving the current solutions (optimizations, typo fixes, etc.).

Comments

@umpirsky
Copy link

ExistsFilterType default choice is ExistsFilter::FALSE, but sometimes it makes more sense to be ExistsFilter::TRUE.

Describe the proposed solution
We can add:

public function configureOptions(OptionsResolver $resolver): void
{
    $resolver
        ->setDefaults([
            'default_choice' => ExistsFilter::FALSE,
        ])
    ;
}

and sort choices accordingly.

Describe alternatives you've considered
Alternatively we can add new DoesNotExistFilterType to achieve this.

I ended up with a simple override to achieve this:

<?php

declare(strict_types=1);

namespace App\Form\Extension;

use Sylius\Component\Grid\Filter\ExistsFilter;
use Sylius\Bundle\GridBundle\Form\Type\Filter\ExistsFilterType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class ExistsFilterTypeExtension extends AbstractTypeExtension
{
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver
            ->setDefaults([
                'choices' => [
                    'sylius.ui.yes_label' => ExistsFilter::TRUE,
                    'sylius.ui.no_label' => ExistsFilter::FALSE,
                ],
                'choice_values' => [
                    ExistsFilter::TRUE,
                    ExistsFilter::FALSE,
                ],
                'data_class' => null,
                'required' => false,
                'placeholder' => false,
            ])
        ;
    }

    public static function getExtendedTypes(): iterable
    {
        return [ExistsFilterType::class];
    }
}
@NoResponseMate NoResponseMate transferred this issue from Sylius/Sylius May 25, 2023
@NoResponseMate NoResponseMate added Enhancement Minor issues and PRs improving the current solutions (optimizations, typo fixes, etc.). DX Issues and PRs aimed at improving Developer eXperience. labels Jun 16, 2023
@NoResponseMate
Copy link
Contributor

Hi @umpirsky

Sorry for the late response.
That's a valid point but would be nice to have it configurable on per grid level.
IMO the alternative is an overkill.

Could you open a PR with the changes?
Thanks in advance and cheers 🍻

@umpirsky
Copy link
Author

How would config look like? Can you provide an example?

@NoResponseMate
Copy link
Contributor

I was thinking of something like:

filters:
  exists:
    type: exists
    default_value: true

But I've dug a bit deeper and that's already possible out of the box, the only problem is there is no correlation between the value set here and the value selected on the form.
With the above config, the criterium gets applied, but the form still says No.

Again there's a way of configuring the default value of the form, but:

  1. it's hacky
  2. we need to set seemingly the same data in two places in a different form
filters:
  exists:
    type: exists
    default_value: true
    form_options:
      empty_data: '1' # has to be a string

I don't have any good idea ATM how to tackle this or even if it can be solved since the empty_data set on the form is bound with it and its data transformers, while the default_value has to be digestible by the filter itself.

@umpirsky
Copy link
Author

Interesting hack, I will give it a shot...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DX Issues and PRs aimed at improving Developer eXperience. Enhancement Minor issues and PRs improving the current solutions (optimizations, typo fixes, etc.).
Projects
None yet
Development

No branches or pull requests

2 participants