简体   繁体   中英

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.

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->add(new \\App\\Middleware\\OldInputMiddleware($container)); ):

After, with Twig Module installed you can call the old value of input (after post failing):

value="{{old.Name}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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