The problem with your approach is that the Symfony Form component will display form elements with an identifier that will be duplicated if you place the same form twice on your page. You may also encounter the csrf_token problem. The bottom line is that forms are not intended to be duplicated.
Here is what I will do. Create a twig template containing your paginator form without using Symfony \ Form, i.e. Create all the form elements statically and pass it a pointer object (or array) to get the data instead of using form_widget() . Something like that:
<form action="{{ path(app.request.attributes.get('_route') }}" method="POST"> <select name="paginator[per_page]"> {% for per_page in paginator.papers_per_page %} <option value=""{{ per_page }}">{{ per_page }}</option> {% endfor %} </select> </form>
The form action will automatically send data to your current route, so you can insert it into different actions and it will transfer data to the same action. In POST, you can simply create a pointer object with post data and then add it as form data. After that, you simply use isValid () as usual.
In your controller you can get the following data:
use Symfony\Component\HttpFoundation\Request; // ... public function PaperController() { public function listAction(Request $request) { if ($request->getMethod() == 'POST') { $data = $request->request->get('paginator'); $paginator = new Paginator($data); $form = new PaginatorFormType(); $form->setData($paginator); if ($form->isValid()) { // ... } } } }
You can easily insert a form into your view as follows:
{{ include 'AcmeDemoBundle:Form:paginator.html.twig' with { 'paginator': paginator } }}
Basically you just use the Form component in your controller for validation. If you want to set some default values ββor add additional arguments, you can create a macro from this template, but that should be enough for your use case. Maybe someone has a better solution, but I did this with a similar problem in one of my projects.
dbrumann
source share