dqa35710
2016-10-26 15:30
采纳率: 0%
浏览 38
已采纳

Symfony表单,将ManyToMany关系显示为复选框列表而不是选择

I want to show a ManyToMany relation in an entity as a list of checkboxes in a form instead of a select with the multiple selection.

In my entity I have this:

/**
 * @var ArrayCollection
 *
 * @ORM\ManyToMany(targetEntity="Language")
 * @ORM\JoinTable(name="courses_languages",
 *      joinColumns={@ORM\JoinColumn(name="course_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="language_id", referencedColumnName="id")}
 *      )
 */
private $languages = [];

And in my FormType for this entity I have this:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('languages', ChoiceType::class, [
            'multiple' => true,
            'expanded' => true
        ])
    ;
}

But when setting multiple and expanded to true I receive this error:

Unable to transform value for property path "languages": Expected an array.

I've investigated and it is in Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToValuesTransfomer in function transform

public function transform($array)
{
    if (null === $array) {
        return array();
    }

    if (!is_array($array)) {
        throw new TransformationFailedException('Expected an array.');
    }

    return $this->choiceList->getValuesForChoices($array);
}

The type of the array is Doctrine\ORM\PersistentCollection so it fails in checking is_array.

How could I fix this? Thank you

1条回答 默认 最新

相关推荐 更多相似问题