Symfony 2表单有两个提交按钮

Raz*_*432 14 forms button symfony

所以,我有一个表格,每行都有一个复选框,如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

现在,在我的控制器中,我想检查按下了什么按钮.我怎么做?

在我的其他形式,由symfony生成它很容易,因为我有一个表单对象和一个非常有用的方法:

if ($form->get('delete')->isClicked()) {
    // delete ...
}
Run Code Online (Sandbox Code Playgroud)

我想知道正确的方法来做到这一点.

谢谢.

paa*_*man 17

从Symfony 2.3开始,您可以:

形成:

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

控制器:

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
}
Run Code Online (Sandbox Code Playgroud)

见这里:http://symfony.com/blog/new-in-symfony-2-3-buttons-support-in-forms


red*_*rdo 11

你可以使用例如

    $request = $this->get('request');
    if ($request->request->has('delete'))
    {
        ...
    }
Run Code Online (Sandbox Code Playgroud)