Reference a variable inside expression in Twig - symfony

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)) }}

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).

twig form multiple block in symfony4

I have a form with block of the different input field.
When I use, standard twig function I get one block:
{{ form(form) }}
If I want to change something inside form I use, for example:
{{ form_start(form) }}
{{ form_widget(form.firstName) }}
{{ form_widget(form.lastName) }}
{{ form_end(form) }}
And everything is fine with this, but here I use JS for adding multiple block of the same field in the form (like can be possible to add multiple person in one form). When I want to edit data, I catch all data from the DB, of course, and want to show blocks in the twig.
{{ form_start(form) }}
{# somehow start loop data from the DB here #}
<div class='block'>
{{ form_widget(form.firstName) }}
{{ form_widget(form.lastName) }}
</div>
{# somehow end loop data from the DB here #}
{{ form_end(form) }}
Is it possible in the Twig, or I should use old school here?
For all who google for same question, answer is here:
http://symfony.com/doc/current/form/form_collections.html

Symfony splitted embedded form

I work with a symfony embedded form to manage my translated fields for a given entity. My principal entity has a boolean field and I have multiple text fields for each translation.
I do not use the translatable doctrine extension and I do not want to use it.
In my FormType, I use a CollectionType to embed the translated fields in my form, and in the template, I use the form theme to customize the HTML.
This is my problem : I would like to group fields in my form to optimize UX but once I call the form_widget on my field, I cannot use it a second time. I would like to show 2 fields for a given language and then another field for another language further in the form. How can I solve my problem ?
This is a twig example that illustrate my problem.
{% form_theme form _self %}
{% block _service_translations_entry_widget %}
<div>
{% if name == 0 %}
<div class="s12 m6 l6">
{{ form_label(form.title) }}
{{ form_errors(form.title) }}
{{ form_widget(form.title) }}
</div>
<div class="s12 m6 l6">
{{ form_label(form.subtitle) }}
{{ form_errors(form.subtitle) }}
{{ form_widget(form.subtitle) }}
</div>
{% endif %}
{% if name == 1 %}
<div class="s12 m12 l12">
{{ form_label(form.desc) }}
{{ form_errors(form.desc) }}
{{ form_widget(form.desc) }}
</div>
{% endif %}
</div>
{% endblock %}
{% block body %}
{{ form_start() }}
{{ form_errors(form) }}
<div>
<div class="s12 m12 l12">
{{ form_label(form.doubleBlock) }}
{{ form_errors(form.doubleBlock) }}
{{ form_widget(form.doubleBlock) }}
</div>
</div>
<div id="block-1">
{{ form_widget(form.translations) }}
</div>
<div>
<div class="s12 m6 l6">
{{ form_label(form.activedStyle) }}
{{ form_errors(form.activedStyle) }}
{{ form_widget(form.activedStyle) }}
</div>
<div class="s12 m6 l6">
{{ form_label(form.checkoutOption) }}
{{ form_errors(form.checkoutOption) }}
{{ form_widget(form.checkoutOption) }}
</div>
</div>
<div id="block-2">
{{ form_widget(form.translations) }}
</div>
{{ form_widget(form.save) }}
{{ form_rest(form) }}
{{ form_end(form) }}
{% endblock body %}
As you say, you can’t reuse a field.
My suggested approach would be to add an additional field to the form, translations_extra, which either isn’t mapped to or persisted by the entity. Extract the data in the controller on form submission and then add it to the translations collection before persisting.

Getting a specific value from Twig array (using Symfony2)

So I can do this:
{% for x in data %}
{{ x.label }}: {{ x.value }}<br />
{% endfor %}
But I want to do this kind of thing to get one specific value:
{{ data['label' }}
I can't see how to do it, but it must be possible.
http://twig.sensiolabs.org/doc/functions/attribute.html
{{ attribute(data,'label') }}

Symfony Twig Form Theming of specific fields

I have a custom form field type and an associated form theme for it. On one page I have a lot of these fields, but one of the fields in particular I want to change.
Is there any way to theme certain fields of the same type (and in the same file) differently?
A simplified example:
form_fields.html.twig: (local theming file)
{% block my_dropdown_row %}
<div>
{{ form_label(form) }}
{{ form_widget(form) }}
{{ form_errors(form) }}
</div>
{% endblock %}
In my form template (all these fields have the same type - my_dropdown
{{ form_row(form.selectionA) }}
{{ form_row(form.selectionB) }}
{{ form_row(form.selectionC) }}
{{ form_row(form.final_selection) }}
How can I style the final field differently to the others? There is a lot of code in these widgets so less duplication the better.
This can be done. Here is how:
http://symfony.com/doc/current/cookbook/form/form_customization.html
Old question and already answered, but would be nice to have more detailed info.
{% form_theme form _self %}
{% block _product_name_widget %}
<div class="text_widget">
{{ block('form_widget_simple') }}
</div>
{% endblock %}
{{ form_widget(form.name) }}
Where block name _product_name_widget stands for
_ form type(block prefix)_ form item name_render function
http://symfony.com/doc/current/form/form_customization.html#how-to-customize-an-individual-field

Resources