CountryField allowMultipleChoices
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()
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.
@thomaspeissl you should already be able to do that with:
- https://symfony.com/bundles/EasyAdminBundle/current/fields/CountryField.html
- The includeOnly() method of Country fields
- Set some form options (e.g.
multiple => trueandexpanded => true)
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.
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).
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;
@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