Description
Q | A |
---|---|
Bug report? | yes |
Feature request? | no |
BC Break report? | no |
RFC? | no |
Symfony version | 2.7 |
Hi there!
This morning I faced an issue in EasyAdminBundle related to form themes and form.parent
check in Twig.
Briefly, the problem happen if we have a simple form with a field named parent
. The situation gets worse depending on whether the parent
field has children or not.
First, let's see the FormView
class, especially the parent
property and \ArrayAccess
methods:
symfony/src/Symfony/Component/Form/FormView.php
Lines 19 to 126 in 61b7534
In PHP context there's no problem, the main form view has the $form->parent
property and its children's fields ($form['parent']
), each one accessible unmistakably by syntax. But, in Twig context, this access/check {% if form.parent is null/empty %}
causes a naming collision. The parent
attribute is resolved by Twig following the steps below:
https://twig.symfony.com/doc/2.x/templates.html#variables
For convenience's sakeform.parent
does the following things on the PHP layer:
- check if
form
is an array andparent
a valid element; <-- always wins in this particular case- if not, and if
form
is an object, check thatparent
is a valid property; <-- never reached- ...
- if not, and if
form
is an object, check thatgetParent
is a valid method;
Therefore, the parent
child field view is returned instead of the real parent
view (which is null in this case)!
There's some place in the source where this kind of check is done and surely in many bundles and apps:
I'm not sure if there's a workaround for current projects/bundles rely on the Symfony form themes templates. I don't think it is a bug of Twig either, but a design error of the FormView
class, which is specially designed to work in Twig context.
We may need a getter method for this parent
property by now and check {% if form.getParent %}
everywhere avoiding the first rule. Wdyt? thoughts?