I have a simple form with collection of items in it. Just like described here: http://symfony.com/doc/current/cookbook/form/form_collections.html.
The problem is when I add new element (or multiple elements) WITHOUT touching enything in them (not changing theirs inputs) I get an empty collection.
I would like to get a collection of added elements regardless theirs content. And even if there are fields are empty it would be ok.
In my case after form submission I have:
var_dump($request->get('my_form_name'));
/* dump result:
array (size=19)
...
'partners' =>
array (size=1)
0 =>
array (size=2)
'fullname' => string '' (length=2)
'manager' => string '' (length=0)
...
*/
var_dump($form->getData()->getPartners());
// EMPTY!!! WTF? I expect an array of 1 element of Partner class which fields are set to '' and ''
On the other hand if somethig is set then it is ok:
var_dump($request->get('my_form_name'));
/* dump result:
array (size=19)
...
'partners' =>
array (size=1)
0 =>
array (size=2)
'fullname' => string ' NAME HERE ' (length=2)
'manager' => string '' (length=0)
...
*/
var_dump($form->getData()->getPartners());
// ok, we have an array of 1 element which is an object of Partner class
Any ideas how to get rid of that bug?
UPDATED
Partner’s class mapping is:
BundleEntityPartner:
fields:
fullname:
type: string
lenght: 255
nullable: True
manager:
type: string
lenght: 255
nullable: True
manyToOne:
owner:
targetEntity: Step2
inversedBy: partners
joinColumn:
name: owner_id
referencedColumnName: id
field partners
is added to the form of Step2
entity like collection
with allow_add
, allow_delete
, by_reference
set to false
Advertisement
Answer
ok, I found a problem.
I must to set empty_data
in subform (Partner class).
I don’t understand why but when all fields of related class (Partners) are submitted to be ""
(empty) then in method sumbit
of class SymfonyComponentFormForm
the block
if (FormUtil::isEmpty($viewData)) {
$emptyData = $this->config->getEmptyData();
if ($emptyData instanceof Closure) {
/* @var Closure $emptyData */
$emptyData = $emptyData($this, $viewData);
}
$viewData = $emptyData;
}
gives null
in $viewData
for Partner of empty fields. For object submitted to be the Partner with at least one field not empty $viewData
is needed object.
All that is because
$this->config->getEmptyData()
for empty object is Closure
which eventually results to null
, but for Partner
with at least one field not empty it gives directly needed object.