[英]Symfony 3 form : display old inputs values in twig after submitting a form with invalid values
I'm trying to get old inputs values and display it as default values using twig. 我正在尝试获取旧的输入值并使用twig将其显示为默认值。
With something like this : 有这样的事情:
{{ form_widget(form.numeroCIN, {'attr': {'class': 'form-control', value:'{% if form.vars.old.fieldName %} {{form.vars.old.fieldName}} {% endif %}'}}) }}
Create a Middleware Class: 创建中间件类:
class OldInputMiddleware extends Middleware
{ {
public function __invoke ($request, $response, $next)
{
$this->container->view->getEnvironment()->addGlobal('old', $_SESSION['old']);
$_SESSION['old'] = $request->getParams();
$response = $next($request, $response);
return $response;
}
} }
Register it into App.php (my default bootstrap file) 将其注册到App.php(我的默认引导程序文件)
$app->add(new \\App\\Middleware\\OldInputMiddleware($container));
): ):
After, with Twig Module installed you can call the old value of input (after post failing): 之后,安装了Twig模块后,您可以调用旧的输入值(发布失败后):
value="{{old.Name}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.