EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

CountryField allowMultipleChoices

Open 7underlines opened this issue 3 years ago • 6 comments

Short description of what this feature will allow to do: Use the CountryField as <select multiple>.

Example of how to use this feature Here is my current crud crontroller code:

public function configureFields(string $pageName): iterable
    {
        return [
            IdField::new('id')->onlyOnIndex(),
            CountryField::new('country'),
            ChoiceField::new('deliveryCountries')->allowMultipleChoices()->setChoices([
                'Austria' => 'AT',
                'Germany' => 'DE',
                ...
            ])
        ];
    }

With this feature I could write the following code:

CountryField::new('deliveryCountries')->allowMultipleChoices()

7underlines avatar Jun 15 '22 09:06 7underlines

All choice field types should have this basic option I think (country, timezone, locale, etc). Of course for now, I think you could use this "workaround": $countryField->setFormTypeOption('multiple', true). Same for filters.

Ang3 avatar Jun 15 '22 17:06 Ang3

@thomaspeissl you should already be able to do that with:

  1. https://symfony.com/bundles/EasyAdminBundle/current/fields/CountryField.html
  2. The includeOnly() method of Country fields
  3. Set some form options (e.g. multiple => true and expanded => true)

javiereguiluz avatar Jun 16 '22 11:06 javiereguiluz

I tried to use CountryField::new('deliveryCountries')->setFormTypeOption('multiple', true), but it gives me this error: EasyCorp\Bundle\EasyAdminBundle\Field\Configurator\CountryConfigurator::getFlagCode(): Argument #1 ($countryCode) must be of type ?string, array given, called in vendor/easycorp/easyadmin-bundle/src/Field/Configurator/CountryConfigurator.php on line 38

If I change the entity field from array to string then the form field shows up, but on form submit I get this error: Expected argument of type "?string", "array" given at property path "deliveryCountries".

I also tried it with "fresh" databases - delete everything then bin/console doctrine:schema:create, but still the same errors.

7underlines avatar Jun 22 '22 13:06 7underlines

The first part shows like an EasyAdmin issue (we should support multiple countries selection) ... but the second part looks like an application issue. Your setter method should be ?array (if you only support multiple country selection) or string|array|null (if you support single and multiple country selection).

javiereguiluz avatar Jun 22 '22 14:06 javiereguiluz

I tried it with string|array|null and without any type hint at all. But this gives me either this error Symfony\Bridge\Doctrine\Middleware\Debug\Query::setValue(): Argument #2 ($value) must be of type string|int|float|bool|null, array given, called in vendor/symfony/doctrine-bridge/Middleware/Debug/Statement.php on line 48 or that if I change the entity field to string EasyCorp\Bundle\EasyAdminBundle\Field\Configurator\CountryConfigurator::getFlagCode(): Argument #1 ($countryCode) must be of type ?string, array given, called in /home/loguser002/a/agrapack_suppliers/vendor/easycorp/easyadmin-bundle/src/Field/Configurator/CountryConfigurator.php on line 38.

I define the entity field like this: #[ORM\Column(type: 'array', nullable: true)] private $deliveryCountries = [];

or for string #[ORM\Column(type: 'string', length: 255, nullable: true)] private $deliveryCountries;

7underlines avatar Jun 22 '22 14:06 7underlines

@javiereguiluz I repeat myself clearer, all fields with form type ChoiceType should have the option allowMultipleChoices because they are all configured with a custom logic (EA) behind the scene. :smile:

In this case, the configurator does not care about this generic option of choice types, like multiple values. It takes the value as single one:

https://github.com/EasyCorp/EasyAdminBundle/blob/0f31eb2bf8f45569846793f99d1a67ea38db7d05/src/Field/Configurator/CountryConfigurator.php#L38-L39

Ang3 avatar Jun 22 '22 21:06 Ang3