2
votes

I have read the Symfony 2 documentation and I'm trying to make a custom embedded form and I can't understand the provided code in the documentation. Official documentation: http://symfony.com/doc/current/cookbook/form/form_customization.html

Specifically the code that i don't understand is this:

{% form_theme form _self %}

{% block _tasks_entry_widget %}
<tr>
    <td>{{ form_widget(task.task) }}</td>
    <td>{{ form_widget(task.dueDate) }}</td>
</tr>
{% endblock %}

after many tests I've noticed that '_task_entry' is the name of the embedded form (not the name of field in the main form)

Now I'm trying to get what is the 'task' variable, {{ form_widget(task.dueDate) }}

I've tried with the embedded form name again, with the name of the entity field, and with the name of the main form variable but nothing works:

{% form_theme edit_form.lineas _self %}
{% block zb_gestionbundle_lineaalbaran_widget %}
    <div class="large-1 small-1 columns">
        {{ form_widget(form.cantidad) }}
    </div>
    <div class="large-8 small-8 columns">
        {{ form_widget(form.concepto) }}
    </div>
    <div class="large-2 small-2 columns">
        {{ form_widget(form.precio) }}
    </div>
{% endblock %}

{{ form_label(edit_form.lineas) }}
{{ form_errors(edit_form.lineas) }}
{{ form_widget(edit_form.lineas) }}

In summary, What I need to put in {{ form_widget(form.cantidad) }} for make the code works?

Tyvm!!

One possible solution:

After investigate a little more, I've found this code that works!

{% form_theme edit_form _self %}
{% macro prototype(linea) %}
    <div class="large-1 small-1 columns">
        {{ form_widget(linea.cantidad) }}
    </div>
    <div class="large-8 small-8 columns">
        {{ form_widget(linea.concepto) }}
    </div>
    <div class="large-2 small-2 columns">
        {{ form_widget(linea.precio) }}
    </div>
{% endmacro %}

{% for linea in edit_form.lineas %}
    {{_self.prototype(linea)}}
{% endfor %}

I don't know if the documentation is wrong, I leave the answer open for the doubt about the documentation.

1

1 Answers

0
votes

Your solution work ! Just to complete, i had the same problem but the documentation is correct ! Just a little bit difficult to understand.(in my opinion).

To use the documentation solution :

  • you have to know the unique_block_prefix of your embbeded form.
    To do this : add this to your code {{dump(form)}} and search the unique_block_prefix of your embedded form.

  • then you just have to replace the example of documentation like this :

{% form_theme form _self %}
{% block _zb_gestionbundle_lineaalbaran_entry_widget %}

            <div class="large-1 small-1 columns">
                {{ form_widget(form.cantidad) }}
            </div>
            <div class="large-8 small-8 columns">
                {{ form_widget(form.concepto) }}
            </div>
            <div class="large-2 small-2 columns">
                {{ form_widget(form.precio) }}
            </div>
            
{% endblock %}
         
     
<!--block with your html/twig code, form, etc ..-->
{% block your_main_block %}

            ...
          <!--your form-->
            ...
          <!-- the embbeded part -->
           {{form_row(form.lineas)}}
           
           ...

{% endblock %}

To sum up, generally the unique_block_prefix is _(id of your embedded form)_entry_widget
And you just have to replace like the example of the doc.
I hope you understand and i miss nothing(sorry for my english...).