extractor
extractor copied to clipboard
SymfonyForm: ChoiceType issue with choices as (static) function call
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.
Thank you for this PR and issue. It makes it real easy to understand and (hopefully) to fix.
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])
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