4

Can someone explain why i am getting this error? I am using CollectionType for first time. Used documentation. Here is my part of where i am trying to use CollectionType:

$builder->add('cc', CollectionType::class, [
         'required' => false,
         'entry_type' => EmailType::class
])

and here is my request:

{
    "email" => "[email protected]",
    "description" => "test description",
    "subject" => "test",
    "cc" => array:1 [
         0 => "[email protected]"
    ]
}
2
  • Do you map this to a model, i.e. is the result from the form a PHP class? If so, could you post the code for that as well? Commented Feb 13, 2019 at 10:18
  • no, i am just trying to submit api request using formbuilder. actually i check if it works or not. bu without any success. is the request wrong? but when i update request to "cc" => "some string" validation returns that value is not valid @dbrumann Commented Feb 13, 2019 at 10:21

2 Answers 2

4

So my problem was in EmailEntity where cc is string. I have used Data Transformers to fix this problem. Just added:

$builder->get('cc')
        ->addModelTransformer(new CallbackTransformer(
            function ($array) {
                return $array;
            },
            function ($array) {
                return json_encode($array);
            }
));

And don't forget to add use Symfony\Component\Form\CallbackTransformer;

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

Comments

1

I had pretty much the same problem as you, but your exact solution didn't work for me, but you put me on a path to solve it, therefore gave you +1 of course. I wanted to write this as a comment but it was too long. Here is what I had to change your solution to to make it work:

$builder->get('myCollection')
    ->addModelTransformer(new CallbackTransformer(
        function ($array) {
            return $array;
        },
        function ($array) {
            $res = array();
            foreach($array as $item) {
                if ($item === null) {
                    $item = '';
                }
                $res[] = $item;
            }

            return $res;
        }
    ));

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.