Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 form with two submit buttons

So, i have a table with a checkbox on every row like this:

<form name="" action={{ path('mypath') }}" method="post">
 <button name="print">Print</button>
 <button name="delete">Delete</button>
 <table>
  {% for client in clienti %}
   <tr>
       <td><input type="checkbox" name="action[]" value="{{ client.id }}" /></td>
   </tr>

     .
     .
     .
  {% endfor %}
 </table>
</form>

Now, in my controller i want to check what button was pressed. How do i do that?

In my other forms, generated by symfony it is easy because i have a form object and a very helpful method:

if ($form->get('delete')->isClicked()) {
    // delete ...
}

I want to know the right method to do this.

Thank you.

like image 227
Razvan.432 Avatar asked Mar 31 '14 11:03

Razvan.432


2 Answers

Since Symfony 2.3 you can do:

Form:

$form = $this->createFormBuilder($task)
->add('name', 'text')
->add('save', 'submit')
->add('save_and_add', 'submit')
->getForm();

Controller:

if ($form->isValid()) {
   // ... do something

   // the save_and_add button was clicked
   if ($form->get('save_and_add')->isClicked()) {
       // probably redirect to the add page again
   }

   // redirect to the show page for the just submitted item
}

see here: http://symfony.com/blog/new-in-symfony-2-3-buttons-support-in-forms

like image 190
paaacman Avatar answered Oct 23 '22 00:10

paaacman


You can use e.g.

    $request = $this->get('request');
    if ($request->request->has('delete'))
    {
        ...
    }
like image 11
redbirdo Avatar answered Oct 23 '22 02:10

redbirdo