8000 How access to form.parent when the form has child called parent? · Issue #18882 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

How access to form.parent when the form has child called parent? #18882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rafrsr opened this issue May 26, 2016 · 7 comments
Closed

How access to form.parent when the form has child called parent? #18882

rafrsr opened this issue May 26, 2016 · 7 comments
Labels
Feature Form Good first issue Ideal for your first contribution! (some Symfony experience may be required)

Comments

@rafrsr
Copy link
rafrsr commented May 26, 2016

How can know if a form has a parent or not, in a view?

The fast answer is:

{{ form.parent }}

but if the form has a child called parent the above statement always return a result, even when they do not have a real parent.

then the only way to know if has a real parent is something like:

{% set hasParent = (form.parent is not null and (not form.offsetExists('parent') or form.parent != form.offsetGet('parent'))) %}

I'm missing something?

a method hasParent() is needed in the FormView to avoid the \ArrayAccess.

@ianfp
Copy link
ianfp commented Jun 20, 2016

I just ran into the same problem. Example:

In MyFormType.php:

if ($this->shouldHaveParentInput()) {
    $form->add('parent', EntityType::class, [
        // ...
    ]);
}

In the Twig template:

{% if form.parent is defined %}
    {{ form_row(form.parent) }}
{% endif %}

The condition in the Twig template is always true, which causes Bad Things to happen.

@HeahDude
Copy link
Contributor
HeahDude commented Jun 22, 2016

FormView::$parent is a public property, you can access the child in twig using form['parent'].

But I'm 👍 for adding FormView::hasParent() which should be hardcoded in twig to prevent accessing the property: form.hasParent.

@ianfp
Copy link
ianfp commented Jun 22, 2016

@HeahDude Oh... I actually have the inverse of @rafrsr's problem. (I didn't read his original description carefully enough.) Using array access notation will work fine for me. Thanks!

@linaori
Copy link
Contributor
linaori commented Oct 9, 2016

Usually a form type isn't supposed to know about a parent, a parent only does about its children, not sure if you should build your types/templates from this PoV

8000

@apfelbox
Copy link
Contributor

@iltar it is not uncommon to use the check to properly format nested forms.

Even symfony core templates use it: https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig#L256

@yceruto
Copy link
Member
yceruto commented Nov 10, 2017

Well, what about to reintroduce hasParent() and getParent() methods to FormView to fix all these related bugs? even if it looks like a hack, this fix the bug checking by form.hasParent in all current form themes.

b3081e8#diff-f60b55ea46e40b9c4475a1bd361f6940L239

Note that it isn't just about parent view existence, but access through its properties too.

@yceruto
Copy link
Member
yceruto commented Nov 10, 2017

A quick search on the use of form.parent (*.twig) in Github repos:

30,993 code results (maybe some aren't instances of FormView):

https://github.com/search?utf8=%E2%9C%93&q=%22form.parent%22+extension%3Atwig&type=Code&ref=advsearch&l=&l=

of which 5 in symfony/symfony:
https://github.com/search?l=&q=%22form.parent%22+extension%3Atwig+repo%3Asymfony%2Fsymfony&ref=advsearch&type=Code&utf8=%E2%9C%93

fabpot added a commit that referenced this issue Dec 4, 2017
…nd form fields (yceruto)

This PR was merged into the 2.7 branch.

Discussion
----------

[Form][TwigBridge] Fix collision between view properties and form fields

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #18882
| License       | MIT
| Doc PR        | TODO

This introduce a new Twig test function `rootform` that guarantee the right access to the `parent` property of the form view. The rest of the properties (`vars` and `children`) are not used at least inside Symfony repo.

I've chosen this solution because it doesn't [affect the design of the form view class/interface](https://github.com/symfony/symfony/pull/19492/files#diff-f60b55ea46e40b9c4475a1bd361f6940R168) and because [the problem happen only on Twig](https://github.com/twigphp/Twig/blob/fd98722d15996561f598f9181dbcef8432e9ff85/lib/Twig/Extension/Core.php#L1439-L1447).

More details about the problem here:
* #24892
* #19492
* #23649 (comment)

_if this is approved_ we should update also:
* [`foundation_5_layout.html.twig`](https://github.com/symfony/symfony/blob/336600857b8bb47d5a7ba3d1924a0e7a3e2af7a8/src/Symfony/Bridge/Twig/Resources/views/Form/foundation_5_layout.html.twig#L321-L326) in `3.3` (done in #25305)
* [`bootstrap_4_layout.html.twig`](https://github.com/symfony/symfony/blob/76d356f36a692dd8ad796de363484c97d6731d1f/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig#L176) in `3.4` (done in #25306)

Commits
-------

8505894 Fix collision between view properties and form fields
@fabpot fabpot closed this as completed Dec 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Form Good first issue Ideal for your first contribution! (some Symfony experience may be required)
Projects
None yet
Development

No branches or pull requests

8 participants
0