首页 > 解决方案 > Symfony 中的自动页面返回链接

问题描述

我想找到一种正确的方法,在 Symfony 5 的表单上的提交按钮旁边添加一个取消关闭按钮。根据我在互联网上的研究,我找到了各种方法:

我不知道这种类型的功能在框架上是不可能的。

谢谢你的帮助。

标签: formssymfonybutton

解决方案


我终于根据 emix 的建议找到了解决方案。

所以是的,显然,让我们忘记 js 选项。

要访问 twig 表单模板中的变量,我只需在 Controller 中传递一个变量“route_back”:

$view = $form->createView();
$view->vars['route_back']='account';
return $this->render('home/index.html.twig', [
    'form' => $view
]);

然后,在树枝表单模板中添加动态插入链接:

{%- block form_end -%}
    <div class="row mt-5">
        <div class="col-6 text-right">
            <a class="btn-secondary btn w-75" href="{{ path(route_back) }}">{% trans %}Cancel{% endtrans %}</a>
        </div>
        <div class="col-6 text-left">
            <button type="submit" id="account_save" name="account[save]" class="btn-primary btn w-75">{% trans %}Save{% endtrans %}</button>
        </div>
    </div>
{%- endblock form_end -%}

在类型表单上,我当然删除了官方文档中推荐的提交按钮:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', TextType::class)
        ->add('email', EmailType::class)
    ;
}

推荐阅读