schingeldi
schingeldi

Reputation: 910

How to identify a hidden form field in Symfony2 / Twig

I am new in Symfony2 and try to render a form. The basics are clear, but I have a problem with hidden form fields.

I try to render each form widget in a different table cell. But when using a for loop, the first cell is filled with the _token widget. But I like to render the hidden fields afterwards with form_rest.

<table>
  <tr>
    {% for element in form %}
      <td class="header">{{ form_widget(element) }} </td>
    {% endfor %}
  </tr>
</table>

What I am looking for is something like

<table>
  <tr>
    {% for element in form %}
      {% if element.type != 'hidden' %} 
        <td class="header">{{ form_widget(element) }} </td>
      {% endif %}
    {% endfor %}
  </tr>
</table>

Can anyone help me with that? Thanks a lot.

Upvotes: 1

Views: 4129

Answers (4)

mike
mike

Reputation: 51

For Symfony 2.0 you used to do:

{% if "hidden" not in element.vars.types %}

For Symfony 2.1 you've got to do:

{% if "hidden" not in element.vars.block_prefixes %}

It took me ages to work this out because there is no documentation about it at all. I'm also bemused as to why you can't just do:

{% if "hidden" element.vars.type %}

I mean, isn't this the most obvious way?

Upvotes: 5

Fran
Fran

Reputation: 120

Try this:

<table>
  <tr>
    {% for element in form %}
      {% if "hidden" not in element.vars.types %}
        <td class="header">{{ form_widget(element) }} </td>
      {% endif %}
    {% endfor %}
  </tr>
</table>

Upvotes: 0

Asish AP
Asish AP

Reputation: 4441

Try this below code

<table>
  <tr>
    {% for element in form %}
      {% if not element.ishidden %} 
        <td class="header">{{ form_widget(element) }} </td>
      {% endif %}
    {% endfor %}
  </tr>
</table>

Hope this helps you. Happy coding!!!

Upvotes: 2

Dieter
Dieter

Reputation: 1710

A bit of a shot in the dark, but maybe you can use the label in the if statement?

Sort of like {% if form_label(element) != "hidden" %}?

Upvotes: 0

Related Questions