Reputation: 774
I have a small html form in my twig that helps me to filter results, and I would like when the user add a value as an example foo
and submit, it keeps the value he/she just entered after the page refresh.
I have been searching and couldn't find thing that would make it work, or I'm doing this really badly. My only soltution is now to do it in JS.
I will show you my code
my form in my twig
<form action="{{ path('st_backoffice_commerce_offre') }}" method="get">
<input name="filter" type="text" value="" id=userInput">
<button type="submit" class="btn btn-default">Filtrer</button>
</form>
my controller where I setup the filter
variable
private function resultsAction(Request $request, User $user, $type, $archive)
{
$em = $this->getDoctrine()->getManager();
$paginator = $this->get('knp_paginator');
$filter = $request->query->get('filter');
$qb = $em->getRepository("STUserBundle:Operation")->getQueryByTypeAndPro($type, $user, $archive, $filter);
$results = $paginator->paginate(
$qb,
$request->query->get('page',1),
$request->query->get('limit',50),
[
'defaultSortFieldName' => 'opn.dateCreation',
'defaultSortDirection' => 'desc'
]
);
return array("results" => $results, "archive" => $archive);
}
public function offreAction(Request $request, User $user, $archive = false)
{
return $this->resultsAction($request, $user, Operation::OFFRE_COMMERCIALE, $archive);
}
and the JS code I have found to try to make that work
var $userInput;
$("filter").on("change", function(){
$userInput = $("filter").val();
});
I tried as well PHP code into twig but....it wasn't a good idea as I could not make it work. So i'm stuck with a rather simple question I had, but couldn't really achieve my input to stay after refresh in twig.
Upvotes: 1
Views: 1445
Reputation: 229
You can access the request object by twig according the documentation : https://symfony.com/doc/current/reference/twig_reference.html#app
So maybe you can try this :
<input name="filter" type="text" value="{{ app.request.query.get('filter') }}" id=userInput">
Upvotes: 3