2

I am trying to make a multiple-select ChoiceType in my Symfony form. However, I am getting the following error:

Unable to transform value for property path "[organisations]": Expected an array.

Note: If I change the name of the component from organisations to anything else, it's rendering the form correctly.

As far as I can see, organisations is an array:

/**
 * @var Organisation[]
 * @ORM\ManyToMany(targetEntity="Booking\Entity\Organisation", inversedBy="users")
 * @ORM\JoinTable(name="users_organisations")
 */
protected $organisations;

Here's the form:

$organisations = $doctrine->getRepository('Booking\Entity\Organisation')->findAll();

$builder
    ->add('organisations', ChoiceType::class, array(
        'choices' => $organisations,
        'choice_label' => function($organisation, $key, $index) {
            return $organisation->getName();
        },
        'multiple' => true
    ))

What am I doing wrong?

6
  • Can you post the entire exception thrown? This is probably not because of the model but rather what data you submit to the form field. Commented Mar 10, 2016 at 10:02
  • @Martin I just added some extra details to the question. If I change organisations to anything else, the form is rendered correctly Commented Mar 10, 2016 at 10:07
  • Well, can you post the entire exception trace stack and you form definition? Commented Mar 10, 2016 at 10:12
  • 3
    Why won't you use EntityType instead? It would be simpler. Commented Mar 10, 2016 at 10:40
  • @dragoste Using EntityType did the trick. Thank you Commented Mar 10, 2016 at 10:43

1 Answer 1

3

Use EntityType instead.

$builder
    ->add('organisations', EntityType::class, array(
        'choices' => $organisations,
        'choice_label' => function($organisation, $key, $index) {
            return $organisation->getName();
        },
        'multiple' => true
    ))

I posted this just because people (like me) don't usually read all the comments. Merit goes to Jakub.

Sign up to request clarification or add additional context in comments.

3 Comments

It will be prefect to mention about your Github issue too => github.com/symfony/symfony/issues/23192
Nice find ;) I don't think it's useful, I just misread the doc.
But error is same also same case of try to make select choose with Entities classes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.