I try to create a custom form control for editing a EntityCollection in my Entity
My customFormType is like :
$hiddenInput = $builder->create($builder->getName(), TextType::class); $hiddenInput->addModelTransformer(new CallbackTransformer( function ($idsString) use ($repository) { dump($idsString);// $ids = array_filter(explode(',', $idsString));// $a = array_map(function($id) use ($repository) {// if ($id !== null && strlen($id) > 0) {// return $repository->find($id);// }// }, $ids);// return $a; return []; }, function ($ids) use ($repository) { $collection = new ArrayCollection(); foreach ($ids as $id) { $collection->add($repository->find($id)); } return $collection; } )); $hiddenInput->addViewTransformer(new CallbackTransformer( function ($idsArray) { if ($idsArray === null) { return ''; } return implode(',', $idsArray); }, function ($idsString) { return explode(',', $idsString); } )); $builder->add($hiddenInput);
My first issue is in my ModelTransformer, $idsString
still get null value ... I didn't find why ...
My second issue is if I submit the form and dump my entity I see this:
I didn't understand why new value for the collection is added in a new key ...I didn't understand how it's possible ...( my adder can't take an array, my setter ... why merge old value with the news it makes no sense ... so my entity seem right, and only the form has issue :/ )