Duplicate form row in twig template symfony 4 - symfony

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

Related

Symfony form: testing widget breaks render of the widget

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

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

Symfony2 presenting an array value as a translated text

it does not seem possible to use a value i am getting right from an array, as a translated text....so when i have
{% for key,value in ratings %}
<th scope="row">
{% trans %}
{{ value.type }}
{% endtrans %}</th>
<td ><div class="rating" id= "{{ value.type }}" data-rating="{{ value.ratingaverage }}"
thread="{{thread_id}}" rating_readonly= "{{ value.readOnly }}" route="{{ path('addrating') }}" ></div> </td>
{% endfor %}
i get the error
A message must be a simple text in TrackerMembersBundle::rating.html.twig at line 92
what is meant here, is the line with
{% trans %}
{{ value.type }}
{% endtrans %}
i cannot seem to be able to use trans upon the value coming directly from array? the value would be for example "file.quality"
Perhaps its better when you search here before.
Symfony2+Twig, variable in translation return "A message must be a simple text"
The mistake is the variable in the translation block. You have to set an placeholder and replace this with a value.
{% trans with {'%type%':value.type} %}
This is my %type%!
{% endtrans %}

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