Sylius / SyliusGridBundle

Generic data grids for Symfony applications, support Doctrine and custom drivers, sorting, filtering, actions and field types.
MIT License
118 stars 49 forks source link

ExistsFilterType default choice #304

Open umpirsky opened 1 year ago

umpirsky commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

NoResponseMate commented 1 year ago

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 commented 1 year ago

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