Symfony form: testing widget breaks render of the widget - symfony

if I test my widget with the following code the widget does not render; not html widget in my page; only the label html
if I replace {{ form_widget(form, {'attr': {'class': 'form-input form-choice form-checkbox'}}) }} with a dump I get the dump displayed so the if statement works
If I remove the if my widget is rendered so looks like the if statement is breaking the rendering ?
<div class="custom-select">
{% if 'checkbox' in (form_widget(form)) %}
{{ form_widget(form, {'attr': {'class': 'form-input form-choice form-checkbox'}}) }}
{% else %}
{{ form_widget(form, {'attr': {'class': 'form-input form-choice'}}) }}
{% endif %}
</div>

Have you tried to specify form item ?
{{ form_widget(form.inputname) }}

Related

Duplicate form row in twig template symfony 4

I want to duplicate each form row inside my template by using for loop
this is my code, but it's doesn't work, it's returned only the first line with form and those other without form, please could somebody help me ?
{% for commande in commandes %}
<tr><td>{{ form_row(form.id, {'attr': 'value': commande.id} ) }}</td></tr>
<tr><td>{{ form_row(form.number, {'attr': 'value': commande.number} ) }}</td></tr>
<tr><td>{{ form_row(form.date, {'attr': 'value': commande.date} ) }}</td>
<td><button type="submit" name="btn">update</td>
</tr>
{% endfor %}
Forms are sometimes smart and know they have been rendered.
Workaround:
{% set output_id = form_row(form.id, {'attr': 'value': commande.id}) %}
{{ output_id }}
and later on you can access it again:
{{ output_id }}
You can also do this for more than a form row (or for adding markup):
{% set output %}
{{ form_row(form.id) }}
{{ form_row(form.number) }}
{{ form_row(form.date) }}
{% endset %}
{{ output }}
(Alternatively, you can probably set the rendered attribute to false or something, but to be honest, that seems worse to me than capturing the output).

Symfony form theme conflict

I have two forms in the same template page, where I need to apply for each form one specific theme. Unfortunately the first theme override the second one.
So that the second theme is not applied at all.
Another thing, in pages where I only have the form_subscribe form, the second theme is well applied for that form. The problems comes only when I have tow forms in the same page.
First Form:
{% form_theme form '#ezdesign/_form/bootstrap_full_form_theme.html.twig' %}
{{ form_start(form) }}
{{ form_end(form) }}
Second Form:
{% form_theme form_subscribe '#ezdesign/_form/bootstrap_modal_form_theme.html.twig' %}
{{ form_start(form_subscribe) }}
{{ form_end(form_subscribe) }}
It's not necessary to share my template content, anyway:
First theme templae:
{% extends 'bootstrap_4_layout.html.twig' %}
{% block form_row -%}
{%- if compound is defined and compound -%}
{%- set element = 'fieldset' -%}
{%- endif -%}
<{{ element|default('div') }} class="form-group">
{{- form_widget(form) -}}
</{{ element|default('div') }}>
{%- endblock form_row %}
Second Theme template:
{% extends 'bootstrap_4_layout.html.twig' %}
{% block form_row -%}
{%- if compound is defined and compound -%}
{%- set element = 'fieldset' -%}
{%- endif -%}
<{{ element|default('div') }} class="form-group">
<div class="row">
{{- form_label(form, null, {'label_attr': {'class' : 'col-sm-4'}}) -}}
<div class="col-sm-8">
{{- form_widget(form) -}}
</div>
</div>
</{{ element|default('div') }}>
{%- endblock form_row %}
Any idea would be appreciated and voted.
Have your registred your custom form templates in twig config ?
# config/packages/twig.yaml (symfony 4)
# app/config/config.yml (symfony < 4)
twig:
form_themes:
- ...
- '#ezdesign/_form/bootstrap_full_form_theme.html.twig'
- '#ezdesign/_form/bootstrap_modal_form_theme.html.twig'
https://symfony.com/doc/current/form/form_themes.html#applying-themes-to-all-forms
As this could well be a side effect of the caching, and as it seems to be linked to the fact that the two forms are in the exact same template, a way to fix this could be to separate your forms with includes:
some-page.html.twig
{{ include('partial/form.html.twig', { 'form': form }) }}
{{ include('partial/form-subscribe.html.twig', { 'form_subscribe': form_subscribe }) }}
partial/form.html.twig
{% form_theme form '#ezdesign/_form/bootstrap_full_form_theme.html.twig' %}
{{ form_start(form) }}
{{ form_end(form) }}
partial/form-subscribe.html.twig
{% form_theme form_subscribe '#ezdesign/_form/bootstrap_modal_form_theme.html.twig' %}
{{ form_start(form_subscribe) }}
{{ form_end(form_subscribe) }}
This way, you end up with only one form_theme per template, and wouldn't collide your themes as per this comment:
{# this form theme will be applied only to the form of this template #}
Source: https://symfony.com/doc/current/form/form_themes.html#applying-themes-to-single-forms

Symfony2, a label Token appears when using form themes

I'm using Symfony2.6 , and I'm trying to customize form rendering. The problem that when I put {{ form_rest(form) }} into the form , a label Token appears. How can I make it hidden ?
This is the form theme
{% extends 'form_div_layout.html.twig' %}
{% block form_widget_simple %}
<div class="form-group">
{{ form_label(form, null, {'label_attr': {'class': 'control-label'}}) }}
{{ parent() }}
</div>
{% endblock %}
When I remove the block {% block form_widget_simple %} to test what gives , the Token label become hidden.
Edit:
I'd like to know also if it's correct to change the simple widget block and render inside it a label or no.
You've changed the block of the simple widget which shouldn't render a label (and it doesn't by default). If you really need to do it this way, you may check the type variable and do not render label for the hidden type. Something as following:
{% block form_widget_simple %}
<div class="form-group">
{% if type != 'hidden' %}
{{ form_label(form, null, {'label_attr': {'class': 'control-label'}}) }}
{% endif %}
{{ parent() }}
</div>
{% endblock %}
This works fine :
{% extends 'form_div_layout.html.twig' %}
//........
{%- block hidden_row -%}
<div style="display:none">
{{ form_widget(form) }}
</div>
{%- endblock hidden_row -%}

Update from 2.0 to 2.1: Class for <label> gets lost on it's way

I just updated symfony2 from 2.0.16 to 2.1.2 and now I've got the problem, that my class for the label isn't added anymore.
In the Twig-template I include the field like this:
<div class="row{% if form_errors(form.object) %} _error{% endif %}">
{{ form_label(form.object, null, { 'attr': {'class': '_hint'} }) }}
{{ form_widget(form.object, { 'attr': {'class': 'c6'} }) }}
</div>
And my extension of the default form layout looks like this:
{% use 'form_div_layout.html.twig' with field_label as base_field_label %}
{% block field_label %}
{{ block('base_field_label') }}
{% if attr.class is defined and '_hint' == attr.class %}
<div>
some
</div>
{% endif %}
{% endblock %}
The strange thing is, that the attr.class value is set, as the <div> gets rendered. But the class is not added to the <label> anymore.
you should overwrite a form_label block instead of field_label because field_label is deprecated
You should use a label_attr array instead of attr, ie:
{% if label_attr.class is defined and '_hint' == label_attr.class %}

Reference a variable inside expression in Twig

I am displaying a form with Symfony2 and Twig, and need to display some fields in a dynamic order using a loop over an array. The code looks like this:
{% for activity in activities %}
<div class="check">
{{ form_label(tags_form.chactivity{{ activity.id }}) }}
{{ form_widget(tags_form.chactivity{{ activity.id }}) }}
</div>
{% endfor %}
But of course the {{ activity.id }} does not fit here.
How can I use the activity.id (integer) inside the {{ form_label(...) }} and {{ form_widget(...) }} expressions?
You can do this using the attribute function:
{{ form_label(attribute(tags_form, 'chactivity' ~ activity.id)) }}

Resources