Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether field exists in symfony2 form?

Using this manual, I added dynamic field to my form. Now, how can I check existence of this field in my template?

{{ form_start(form) }}
  {{ form_errors(form) }}

  {% if ??? %}     <---------------------------
    {{ form_row(form.myDynamicField) }}
  {% endif %}
{{ form_end(form) }}
like image 952
psylosss Avatar asked Jan 20 '14 11:01

psylosss


3 Answers

What about,

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

You may also need to check if form.myDynamicField is not null.

like image 99
Ahmed Siouani Avatar answered Nov 12 '22 07:11

Ahmed Siouani


I know this question is a few years old, but you could also make it a shorter ternary operator.

{{ form.myDynamicField is defined ? form_row(form.myDynamicField) : null }}

like image 39
Kegan VanSickle Avatar answered Nov 12 '22 07:11

Kegan VanSickle


{{ form_start(form) }}
  {{ form_errors(form) }}

  {% if form.getChildren['myDynamicField'] is defined %}
    {{ form_row(form.myDynamicField) }}
  {% endif %}
{{ form_end(form) }}
like image 1
Piotr Pasich Avatar answered Nov 12 '22 06:11

Piotr Pasich