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 #72

Closed
noofaq opened this issue Jan 2, 2018 · 3 comments · Fixed by #73
Closed

SymfonyForm: ChoiceType issue #72

noofaq opened this issue Jan 2, 2018 · 3 comments · Fixed by #73

Comments

@noofaq
Copy link
Contributor

noofaq commented Jan 2, 2018

When you use ChoiceType in Symfony with options you don't want to translate, you can use a setting 'choice_translation_domain' => false which prevents translation. You can also use choices as a result of a method and provide callbacks for extracting "choice_label" and "choice_value". It seems that something works incorrectly in given example.

ChoiceType configuration

'choice_translation_domain' => false,
'choices' => $this->entityManager->getRepository(Market::class)->findBy([], ['name' => 'asc']),
'choice_label' => function($item) { return $item instanceof Market ? $item->getName() : null; },
'choice_value' => function($item) { return $item instanceof Market ? $item->getId() : null; },

Error result (pointing to the 'choices' configuration line):

 [ERROR] /<path>/...Type.php
         Line: 132
         Message: Form choice is not an array

This should prevent any review of "choices". Despite that there is an error, that choices should be an array (in this case it is result of EntityRepostory:find) what is an additional issue I think.

I am not sure if there is one or two issues here (one: not handling 'choice_translation_domain' correctly and/or not handling calling other functions as a source of choices)

I can of course hack that by adding "@ignore" annotation but it is a bit inconvenient (as we have now 2 places which defines same behavior - options translated or not - and it poses a risk that one thing changed might create configuration inconsistency)

If you need any additional details don't hesitate to ask me.

@Nyholm
Copy link
Member

Nyholm commented Jan 2, 2018

Thank you for a very well explained issue.
I think it is partly solved by #69

I will make sure we fix the rest.

@Nyholm
Copy link
Member

Nyholm commented Jan 2, 2018

I've added a PR for this issue now.

Having this in the form type:

'choices' => $this->entityManager->getRepository(Market::class)->findBy([], ['name' => 'asc']),

.... should generate an error. You should use @ignore or set choice_translation_type=false. Right? Because whatever is fetched from the database will be translated by default.

@noofaq
Copy link
Contributor Author

noofaq commented Jan 3, 2018

choice_translation_domain = false is the setting to be precise

I confirm that when you use entities from DB in general there is no need to do any translations as shown in the extract of ChoceType settings in the opening message (in case translation is needed custom choice_label callable would probably be used for that matter)

@Nyholm Nyholm closed this as completed in #73 Jan 6, 2018
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

Successfully merging a pull request may close this issue.

2 participants