csrf - Site-wide form with Symfony2? -
i have simple form @ site pages: username, password, [sign in]
i tried make with simple html, get
the csrf token invalid. please try resubmit form
idea make form in each action seems bad. practice of doing site-wide forms?
you have way:
first, guess have base template file layout.html.twig , other pages extend it. eg:
// resources/views/layout.html.twig <doc ... bla blah> <title>my site</title> ...(js, css)... <body> <div id="top"> {% render url("site_wide_form") %} </div> {% block content %} {% endblock content %} </body>
you need controller handle form:
//controller/sitewidecontroller.php /** * @route("/some/url/here", name="site_wide_form") * @template("yourbudle:folder:site_wide_form.html.twig") */ public function someaction() { ..... code form, process submission etc ... return ["form"=>$form->createview()] ; }
and template file:
// site_wide_form.html.twig <form action="{{ path("site_wide_form") }}" method="post"> {{ form_widget(form) }} </form>
that's it. read understand render tag: http://symfony.com/doc/2.2/book/templating.html#embedding-controllers
Comments
Post a Comment