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.
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
You can use e.g.
$request = $this->get('request');
if ($request->request->has('delete'))
{
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With