forms - Symfony 中的自动页面返回链接
问题描述
我想找到一种正确的方法,在 Symfony 5 的表单上的提交按钮旁边添加一个取消关闭按钮。根据我在互联网上的研究,我找到了各种方法:
使用 submit_widget 博客在 twig 模板上添加按钮。它可以工作,但我无法更改路线,因为我无法从控制器传输任何变量。
添加一个简单的like with
href="javascript:history.go(-1)"
。高效但丑陋!基于该帖子和该帖子添加具有 formnovalidate 属性的其他提交按钮。但我似乎这个功能在 Symfony 5 中的工作时间更长。
我不知道这种类型的功能在框架上是不可能的。
谢谢你的帮助。
解决方案
我终于根据 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)
;
}
推荐阅读
- reactjs - 尽管使用 React.FC,但在访问子组件时出现打字稿错误
- angular - 在 mat-select 中选择一个值会导致尝试“diff”时出错
- laravel - ReflectionException (-1) 类 App\Http\Controllers\IndexController 不存在
- c# - 如果值为 null,则在我的视图中显示值会引发 ToString 错误
- spring - 从不同实体获取字段值的最佳解决方案
- google-cloud-platform - 如何将云作曲家与计算引擎集成
- c# - WPF ItemsControl 简单示例,用于在上下文菜单的单击处理程序中获取绑定对象
- mysql - django.db.utils.OperationalError: (1366, "Incorrect string value) django mysql
- linux - 如何将文件从 docker 映像移动到计算机上的文件夹中
- amazon-web-services - S3 指标的巨大差异 - Cloudwatch vs AWS S3 CLI vs UI“获取大小”