title: SonataAdminBundle batch update
body: <p>I'm trying to create a feature in the admin dashboard, I have a <code>products</code> table and I want to create these batch features:</p>
  
  <p><strong>Price Update</strong></p>
  
  <p>Select one or more products, click on Price Update, show the products selected with the current prices, in a form with an input text called <code>new_price</code>, type on of these: %10 or -%10 or 5 or -5 [..], click on Update button and update the selected products with the new price</p>
  
  <p><strong>Active</strong></p>
  
  <p>This is almost the same as Price Update but using an input checkbox and de/activate the selected products</p>
  
  <p><strong>Quantity Update</strong></p>
  
  <p>Update the selected products quantity/stock</p>
  
  <p>What I have tried is, override CRUDController, as described <a href="http://sonata-project.org/bundles/admin/2-2/doc/reference/batch_actions.html" rel="nofollow">here</a>, but in the example it just show how to <code>merge</code> objects, overriding the <code>list__batch.html.twig</code> file adding an input option, it does not show how to create a custom template batch</p>
  
  <p>Another thing that I have tried is to override <code>batch_confirmation.html.twig</code> like this:</p>
  
  <pre><code>{% extends base_template %}
  
  {% block actions %}
      &lt;li&gt;{% include 'SonataAdminBundle:Button:list_button.html.twig' %}&lt;/li&gt;
      &lt;li&gt;{% include 'SonataAdminBundle:Button:create_button.html.twig' %}&lt;/li&gt;
  {% endblock %}
  
  {% block tab_menu %}{{ knp_menu_render(admin.sidemenu(action), {'currentClass' : 'active'}, 'list') }}{% endblock %}
  
  {% block content %}
      &lt;div class="sonata-ba-delete"&gt;
          &lt;h1&gt;{% trans with {'%action%': action_label} from 'SonataAdminBundle' %}title_batch_confirmation{% endtrans %}&lt;/h1&gt;
  
          {% if data.all_elements %}
              {{ 'message_batch_all_confirmation'|trans({}, 'SonataAdminBundle') }}
          {% else %}
              {% transchoice data.idx|length with {'%count%': data.idx|length} from 'SonataAdminBundle' %}message_batch_confirmation{% endtranschoice %}
          {% endif %}
  
          &lt;div class="well well-small form-actions"&gt;
              &lt;form action="{{ admin.generateUrl('batch', {'filter': admin.filterParameters}) }}" method="POST" &gt;
                  &lt;input type="hidden" name="confirmation" value="ok"&gt;
  
                  &lt;input type="hidden" name="_sonata_csrf_token" value="{{ csrf_token }}"&gt;
  
                  {% if data.action == "price_update" %}
                      &lt;input type="text" name="new_price" value="BILLION" /&gt;
                      &lt;input type="hidden" name="idx" value="{{ data.idx|json_encode }}"&gt;
                      &lt;input type="hidden" name="all_elements" value="{{ data.all_elements }}"&gt;
                      &lt;input type="hidden" name="action" value="{{ data.action }}"&gt;
  
                      # Show object rows
                  {% else if data.action == "quantity_update" %}
                      # Show quantity_update object rows
                  {% else if data.action == "de_activate_update" %}
                      # Show de_activate_update object rows
                  {% else %} # delete batch action here!
                      &lt;input type="hidden" name="data" value="{{ data|json_encode }}"&gt;
                  {% endif %}
  
                  # Don't know why this form is not displayed
                  &lt;div style="display: none"&gt;
                      {{ form_rest(form) }}
                  &lt;/div&gt;
  
  
                  &lt;button type="submit" class="btn btn-danger"&gt;{{ 'btn_execute_batch_action'|trans({}, 'SonataAdminBundle') }}&lt;/button&gt;
  
                  {% if admin.hasRoute('list') and admin.isGranted('LIST') %}
                      {{ 'delete_or'|trans({}, 'SonataAdminBundle') }}
  
                      &lt;a class="btn btn-success" href="{{ admin.generateUrl('list') }}"&gt;
                          &lt;i class="glyphicon glyphicon-th-list"&gt;&lt;/i&gt; {{ 'link_action_list'|trans({}, 'SonataAdminBundle') }}
                      &lt;/a&gt;
                  {% endif %}
              &lt;/form&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  {% endblock %}
  </code></pre>
  
  <p>Also I have to override the method <code>batchAction()</code> in <code>CRUDController</code> to manage the request</p>
  
  <p>Is this the correct way?</p>