Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony choice type array error: Unable to transform value for property path: Expected an array

Tags:

php

symfony

Why am I getting an error with this form choice set to multiple. This is coming directly off of Symfonys website. All I changed was the variable name.

 $builder->add('genre', 'choice', array(
    'choices' => array(
        'x'   => 'x',
        'y' => 'y',
        'z'   => 'z',
    ),
    'multiple' => true,
));

This is the error:

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

Here is my entity class for this variable:

 /**
 * @var string
 *
 * @ORM\Column(name="genre", type="text", nullable=true)
 */
private $genre;
like image 740
LargeTuna Avatar asked Jul 28 '15 20:07

LargeTuna


1 Answers

I can confirm that the comment by qooplmao solves the issue:

The problem is that your entity field $genre is not defied as an array but as a string.

But when multiple choices are enabled, the form field will provide an array as a result, and not a string.

So you can:

  • map genre as an array instead of a string
  • or disable multiple choices by setting multiple to false

I this specific problem I guess that you want to map genre as an array.

like image 130
Francesco Borzi Avatar answered Oct 19 '22 19:10

Francesco Borzi