【问题标题】:Symfony3 Forms: How to know which button was clicked in the form?Symfony3 Forms:如何知道在表单中点击了哪个按钮?
【发布时间】:2016-09-12 00:21:04
【问题描述】:

Symfony3 Forms:我已经成功地构建和呈现了如下所示的表单:

<form action="/member/john/actions" method="post" name="form">
    <input type="submit" value="Block John" name="block">
    <input type="submit" value="Remove from my friends" name="remove">
    <input type="hidden" value="LeiajURspTa9c8JEUYtvepki0b_CdL9dMWqEZxOYvfk" name="form[_token]" id="form__token">
</form>

当我点击按钮 "Block John""Remove from my friends" 时,控制器会将其路由到所需位置 (member_friend_actions) 并且它能够显示 调试转储值 em> 连同"Submitted!" 文字,在死之前。

我的路由“member_friend_actions”的控制器设置如下:

/**
 * A common post location to catch all operations like add/remove/cancel/block friends
 *
 * @Route("/{username}/actions", name="member_friend_actions")
 * @Method("POST")
 */
public function allActionsFriendAction(Request $request, User $friend)
{
    $form = $this->createAllActionsFriendForm($friend);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {

        //$clicked = $form->getData();
        $clicked = $form->getClickedButton()
        \Doctrine\Common\Util\Debug::dump($clicked);            
        die("Submitted!");

    }

    return $this->redirectToRoute('member_profile', array("username" => $friend->getUsername()));
}

我想知道点击了哪个按钮将它带到这里(在此处阻止或删除;但在其他地方可以有更多按钮)。我尝试使用这些方法:

$form->getData() => 给出 array(0) { } 和
$form->getClickedButton() => 给出 NULL,所以没有帮助。

如何做到这一点?

【问题讨论】:

  • 你好任。如果我的回答确实“解决”了问题,也请单击我的回答旁边的复选标记,将其标记为正确的。谢谢!

标签: php controller symfony symfony-forms


【解决方案1】:

这取决于您如何将 SubmitType 添加到表单中。 例如,如果你使用这样的东西:

->add('block_john', SubmitType::class, array(
        'label' => 'Block John'
))

然后在你的控制器中你可以使用类似的东西:

$form->get('block_john')->isClicked()

查看此链接了解更多信息: http://symfony.com/doc/current/form/multiple_buttons.html

【讨论】:

  • 可能是因为我没有使用单个表单构建器对象创建所有按钮,-&gt;isClicked() 在我的情况下没有返回 true。所以我尝试了类似的方法。
【解决方案2】:

由于我没有使用-&gt;add()符号一次创建所有按钮,而是使用if-else-if-else条件生成并调用相应的方法,如下所示:

$form = $this->createCancelFriendForm($user);

$addFriendForm = $form->createView();

也许这就是 $form-&gt;getData()$form-&gt;getClickedButton()$form-&gt;get('block')-&gt;isClicked() 这样的函数返回空值的原因。

但是因为这个表单还在通过验证并成功进入提交的情况;我只是尝试使用与 Symfony 等效的 if(isset($_POST["block"])) {},如下所示:

if ($form->isSubmitted() && $form->isValid()) {

    if ($request->request->get("block")) {
            // Pass it over to corresponding block handler
            $response = $this->forward('AppBundle:Member:blockFriend', array(
                   'friend'  => $friend,
            ));
        return $response;
    }

    if ($request->request->get("unblock")) {
        // Pass it over to corresponding unblock handler
        $response = $this->forward('AppBundle:Member:unBlockFriend',
            array(
                    'friend'  => $friend,
            ));
        return $response;
    }

    // I can add n number of other checks ( eg: add, remove, accept)
    // here and add their handlers as above
}

...根据需要对我有用。 B)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 2017-06-04
    • 1970-01-01
    • 2021-05-17
    • 2014-08-31
    • 1970-01-01
    相关资源
    最近更新 更多