Symfony selection type array error: cannot convert value for property path: array is expected

Why am I getting an error if several options are selected for choosing a form. This comes directly from the Symfonys site. I changed only the variable name.

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

This is mistake:

Unable to convert value for path property "genre": array expected.

Here is my entity class for this variable:

  /** * @var string * * @ORM\Column(name="genre", type="text", nullable=true) */ private $genre; 
+5
source share
2 answers

I can confirm that the qooplmao comment solves the problem:

The problem is that the field of your $genre object is not considered as an array , but as a string .

But when multiple options are included, the form field will contain an array , not a string.

So you can:

  • map genre as array instead of string
  • or disable multiple options by setting multiple to false

I have this specific problem, I think you want to display genre as array .

+1
source

Try this code:

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

You may have forgotten to remove the comma after 'z' => 'z' and 'multiple' => true.

Remove the comma and try this way

-4
source

All Articles