Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
marcoreni opened this issue Jan 16, 2018 · 3 comments
Open

Comments

@marcoreni
Copy link

As per php-translation/symfony-bundle#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.

@Nyholm
Copy link
Member

Nyholm commented Jan 16, 2018

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

@emri99
Copy link

emri99 commented Apr 30, 2019

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])

@sylvaindeloux
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants