Skip to content

Instantly share code, notes, and snippets.

@Tom32i
Last active March 6, 2018 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tom32i/6599990 to your computer and use it in GitHub Desktop.
Save Tom32i/6599990 to your computer and use it in GitHub Desktop.
A simple way of automatically render Add and Delete buttons to collections through form theming. You just need to implement the "addPrototype(form_id)" Javascript function. #Symfony2 #Form #Collection
{% extends "form_div_layout.html.twig" %}
{% block form_row %}
{% spaceless %}
<div>
{{ form_label(form) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
{% if form.parent.vars.allow_delete|default(false) %}
<button type="button" onclick="this.parentElement.parentElement.removeChild(this.parentElement)">
&times;
</button>
{% endif %}
</div>
{% endspaceless %}
{% endblock form_row %}
{% block collection_widget %}
{% spaceless %}
{{ parent() }}
{% if allow_add|default(false) %}
<button type="button" onclick="addPrototype('{{ id }}')">
&plus;
</button>
{% endif %}
{% endspaceless %}
{% endblock collection_widget %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment