Skip to content

Document Twig test "rootform" #10022

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

Merged
merged 4 commits into from
Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions reference/forms/twig_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,28 @@ array).

<option {% if choice is selectedchoice(value) %} selected="selected"{% endif %} ...>

.. _form-twig-rootform:

rootform
~~~~~~~~

This test will check if the current ``form`` does not have a parent form view.

.. code-block:: twig

{# DON'T DO THIS: this code will fail when the form doesn't have a parent
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to rephrase this sentence... since in no case this code will fail because the FormView has a public parent property. The problem appears when you have also defined a parent form field. The last one will have priority for Twig due the array access ability.

I'm not sure how to explain it in short though.

Copy link
Member Author

@yceruto yceruto Jul 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean... what you expect for form.parent could change depending on whether your form has or not a parent field. If no: you're checking the parent view, if yes: you're checking the parent field

but it defines a normal form field called 'parent' #}

{% if form.parent is null %}
{{ form_errors(form) }}
{% endif %}

{# DO THIS: this code will always work, regardless of the form field names #}

{% if form is rootform %}
{{ form_errors(form) }}
{% endif %}

.. _`twig-reference-form-variables`:

More about Form Variables
Expand Down
14 changes: 14 additions & 0 deletions reference/twig_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,20 @@ selectedchoice
Checks if ``selectedValue`` was checked for the provided choice field. Using
this test is the most effective way.

rootform
~~~~~~~~

.. code-block:: twig

{% if form is rootform %}

``form``
**type**: ``FormView``

Checks if the given ``form`` does not have a parent form view. This is the only
safe way of testing it because checking if the form contains a field called
``parent`` is not reliable.

Global Variables
----------------

Expand Down