I have a form collection like this in Symfony 3.4:
// MainType.php
$builder->add('children', CollectionType::class, ['entry_type' => ChildType::class]);
// ChildType.php
$builder->add('myField', null, ['mapped' => false]);
// plus more fields, mapped to the underlying `Child` entity
// Controller
$form = $this->createForm(MainType::class, ['children' => $children]);
$form->handleRequest($request);
if ($form->isSubmitted() and $form->isValid()) {
// How can I access the data of `myField` here?
}
When doing it the usual way with
$data = $form->getData();
...I'm getting an array of the Child
entities, not the forms themselves.
So the question in other words is: In a form collection, how can I access the child forms , not the child entities ?
I couldn't find a solution anywhere, so I'm posting how I finally solved it:
/** @var Symfony\Component\Form\Form $formChild */
foreach ($form->get('children') as $formChild)
{
$formChild->get('myField')->getData(); // That's it!
}
The basic principle is explained at https://symfony.com/doc/current/form/without_class.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.