Patrick Reck
Patrick Reck

Reputation: 339

Multiple ChoiceType, array expected

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?

Upvotes: 2

Views: 2595

Answers (1)

Topper Harley
Topper Harley

Reputation: 12374

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.

Upvotes: 3

Related Questions