extractor icon indicating copy to clipboard operation
extractor copied to clipboard

SymfonyForm: ChoiceType issue with choices as (static) function call

Open marcoreni opened this issue 6 years ago • 3 comments

As per https://github.com/php-translation/symfony-bundle/issues/143 :

When I use a function or a static function that returns an array for a ChoiceType element, i get a "Form choice is not an array" error.

The extractor should compute the result of the function and use that to extract the translation items.

Uploading a PR containing test case right now.

marcoreni avatar Jan 16 '18 16:01 marcoreni

Thank you for this PR and issue. It makes it real easy to understand and (hopefully) to fix.

Nyholm avatar Jan 16 '18 17:01 Nyholm

I'm not sure this is always possible to compute result as function can return dynamic strings.

Sample:

$countries = [];
if ($options['favorites_countries']) {
    // add favorites countries first
    ..
    // add remainings countries
    ..
}
$builder->add('country', CountryType::class, ['choices' => array_flip($countries)])

As it's a parsing error, avoid using a function call can be a reasonable (temporary?) solution:

$countries = [];
if ($options['favorites_countries']) {
    ....
}
$countries = array_flip($countries);
$builder->add('country', CountryType::class, ['choices' => $countries])

emri99 avatar Apr 30 '19 15:04 emri99

Same here with static array:

    const USER_GENDERS = array(
        'gender.'.self::USER_GENDER_MALE => self::USER_GENDER_MALE,
        'gender.'.self::USER_GENDER_FEMALE => self::USER_GENDER_FEMALE,
    );
    $builder->add('gender', ChoiceType::class, array(
        'choices' => self::USER_GENDERS,
    ));

Error message: Form choice is not an array

sylvaindeloux avatar Dec 08 '20 09:12 sylvaindeloux