繁体   English   中英

Symfony 3表单:在提交具有无效值的表单后,在树枝中显示旧输入值

[英]Symfony 3 form : display old inputs values in twig after submitting a form with invalid values

我正在尝试获取旧的输入值并使用twig将其显示为默认值。

有这样的事情:

{{ form_widget(form.numeroCIN, {'attr': {'class': 'form-control', value:'{% if form.vars.old.fieldName %} {{form.vars.old.fieldName}} {% endif %}'}}) }}

创建中间件类:

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;

}

}

将其注册到App.php(我的默认引导程序文件)

$app->add(new \\App\\Middleware\\OldInputMiddleware($container)); ):

之后,安装了Twig模块后,您可以调用旧的输入值(发布失败后):

value="{{old.Name}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM