我正在尝试获取旧的输入值并使用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 Module后,你可以调用input的旧值(post失败后):
value="{{old.Name}}