6

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;
5
  • 6
    With multiple choices being available the form will create an array of results but your "genre" property is only a string. You will either need to change the mapping for genre to an array or remove the 'multiple' => true. Commented Jul 28, 2015 at 20:42
  • Thanks for the feedback Commented Jul 28, 2015 at 20:49
  • Did you get it to work? Commented Jul 29, 2015 at 6:03
  • 3
    'mapped' => false makes it work Commented Nov 10, 2016 at 20:04
  • 1
    $builder->get('genre')->resetViewTransformers(); and works with me Commented Jan 17, 2018 at 9:55

5 Answers 5

1

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.

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

Comments

0

My solution for bypass error, juste use this code.

explode("|",$response['quest1'])

use explode for solve you problème

Im use | separatore you can replace | by ,

example code in under

 $builder->add('genre', 'choice', array(
'choices' => array(
    'x'   => 'x',
    'y' => 'y',
    'z'   => 'z',
),
  'multiple' => true,
   explode("|",$response['genre']),
));

Comments

0

I solve the problem with 'data' option

    ->add('flags', ChoiceType::class, [
        'choices' => [Agent::FLAGS],
        'expanded' => true,
        'multiple' => true,
        'required' => false,
        'data' => Agent::FLAGS,
    ])

i am use a bitwise flags, so the database type is a int, and the screen info is a checkbox with a lot of checkboxes.

using the 'data' i can say to symfony what type of data i will use and everthing works.

Comments

0

public function getGenre(): array { ...

Comments

0

Just add data array (empty array if you don't expect any checkbox to be selected by default, or all the values you want to be selected by default )''multiple' => true, data'=>['']

Comments

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.