EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

LanguageField doesn't support multiple choices

Open Seb33300 opened this issue 9 months ago • 4 comments

Describe the bug LanguageField doesn't support allowMultipleChoices even when trying to enforce it with the native option:

->setFormTypeOption('multiple', true)

This results in an exception.

(OPTIONAL) Additional context CountryField is very similar to LanguageField and supports the multiple option.

Seb33300 avatar Apr 28 '24 09:04 Seb33300

Hey @Seb33300 Can you please elaborate a bit more? Version, exception, or the code you're trying etc..

I just tried and here is the result;

Code: Screenshot 2024-05-06 at 16 08 20 Result: Screenshot 2024-05-06 at 16 08 38

emnsen avatar May 06 '24 14:05 emnsen

Can you confirm the code property in your entity is an array?

Because when I try your code, I have this error:

EasyCorp\Bundle\EasyAdminBundle\Field\Configurator\LanguageConfigurator::getLanguageName(): Argument #1 ($languageCode) must be of type string, array given, called in \vendor\easycorp\easyadmin-bundle\src\Field\Configurator\LanguageConfigurator.php on line 46

Seb33300 avatar May 06 '24 14:05 Seb33300

Oh, yes, you're right. I hadn't tried changing the property type to an array. Now, I'm getting the same error as well. Sorry, my bad; I didn't check properly.

emnsen avatar May 06 '24 14:05 emnsen

Hello,

I have the same problem when I want to store several languages in a json column in my entity.

For the moment I have corrected it like this:

public function configureFields(string $pageName): iterable
{
        $languages = Languages::getNames();
        // fields
        yield ChoiceField::new('languages', 'Spoken languages')
            ->setChoices($languages)
            ->setFormTypeOptions([
                'placeholder' => '-- Choose language--',
                'choice_label' => function ($choice, string $key, mixed $value): string {
                    return mb_strtoupper($key) . ' - ' . ucfirst($choice);
                },
                'multiple' => true,
            ])
            ->formatValue(function($value, Destination $destination) {
                if ($value) {
                    $locale = $this->getContext()->getRequest()->getLocale();
                    $languages = Languages::getNames($locale);
                    $langs = array_map(function($lang) use ($languages, $locale) {
                        $code = array_search($lang, $languages);
                        return ucfirst(Languages::getName($code, $locale));
                    }, $value);
                    return implode(', ', $langs);
                }
                return null;
            })
        ;
}

INFOTECH974 avatar Jul 03 '24 11:07 INFOTECH974