0

I'm new to Symfony, and I'm trying to filter my table with a search field. I'm using KnpPaginatorBundle to paginate and sort my table, and I've created a form to filter my request ( method GET )

It is generally working, but when I use spaces or underscores in my search input, it doesn't work, I assume there is something to do about the GET method and the way to encode the text, but I don't know how.

Here's my code :

View :

<div class="row">
        <div class="well row">
            <form action="" method="get">

                <div class="col-md-2">
                    <label for="famille">Famille d'articles</label>
                    <select name="famille">
                        <option value="0">Toutes</option>
                        {% for famille in listFamilles %}
                            <option value="{{ famille.id }}" {% if data.famille is defined %} {% if famille.id == data.famille %} selected {% endif %} {% endif %}>{{ famille.nom }}</option>
                        {% endfor %}
                    </select>
                </div>

                <div class="col-md-4">
                    <input type="checkbox" name="rds" {% if data.rds == 1 %} checked {% endif %}>
                    <label for="rds" style="margin-left:5px">Montrer les articles en rupture de stock</label>
                </div>

                <div class="col-md-4">
                    <label for="recherche">Recherche</label>
                    <input name="recherche" style="width:100%" type="text" placeholder="Recherche" {% if data.recherche is defined %} value="{{ data.recherche }}" {% endif %}>
                </div>

                <div class="col-md-2" style="text-align:center">
                    <button type="submit" class="btn btn-primary">Rechercher</button>
                </div>

            </form>
        </div>
        <div class="well row">
            <table class="table table-bordered table-striped" style="width: 100%" cellspacing="0">
                <thead>
                    <tr>
                        <th>{{ knp_pagination_sortable(listArticles, 'Référence client', 'a.ref_article') }}</th>
                        <th>{{ knp_pagination_sortable(listArticles, 'Référence interne', 'a.ref_logistique') }}</th>
                        <th>{{ knp_pagination_sortable(listArticles, 'Famille', 'f.nom') }}</th>
                        <th>{{ knp_pagination_sortable(listArticles, 'Libellé', 'a.libelle') }}</th>
                        <th>{{ knp_pagination_sortable(listArticles, 'Alerte', 'a.stock_alerte') }}</th>
                        <th>{{ knp_pagination_sortable(listArticles, 'Stock', 'a.stock_actuel') }}</th>
                    </tr>
                </thead>
                <tbody id="bodyListeArticles">
                    {% for article in listArticles %}
                        <tr>
                            <td><a href="{{ path('gr_bo_modif_article', {'article_id': article.id}) }}">{{ article.refArticle }}</a></td>
                            <td>{{ article.refLogistique }}</td>
                            <td>{{ article.famille.nom }}</td>
                            <td>{{ article.libelle }}</td>
                            <td>{{ article.StockAlerte }}</td>
                            <td>{{ article.StockActuel }}</td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>

            <div class="navigation text-center">
                {{ knp_pagination_render(listArticles) }}
            </div>

        </div>

    </div>

Controller :

public function listeAction(Request $request) {
        if ($this->get('security.authorization_checker')->isGranted('ROLE_OPERATEUR')) {
            $session = $request->getSession();
            if ($session->get('client_id')) {
                $clientId = $session->get('client_id');
            } else {
                $request->getSession()->getFlashBag()->add('info', 'Vous devez sélectionner un client pour accéder à la liste de ses articles.');
                return $this->redirectToRoute('gr_bo_liste_clients');
            }
        } elseif ($this->get('security.authorization_checker')->isGranted('ROLE_SUPERCOLLABORATEUR') || ($this->get('security.authorization_checker')->isGranted('ROLE_COLLABORATEUR') && $this->getUser()->getListeArticles())) {
            $clientId = $this->getUser()->getClient()->getId();
        } else {
            $request->getSession()->getFlashBag()->add('info', 'Vous n\'avez pas les permissions requises pour accéder à cette page.');
            return $this->redirectToRoute('gr_bo_liste_commandes');
        }
        $em = $this->getDoctrine()->getManager();
        $data = [];
        $data['clientId'] = $clientId;

        if ($request->query->getAlnum('recherche')) {
            $data['recherche'] = $request->query->getAlnum('recherche');
        }

        if ($request->query->getAlnum('famille') && $request->query->getAlnum('famille') != "0") {
            $data['famille'] = $request->query->getAlnum('famille');
        }


        if ($request->query->getAlNum('rds') == "on" || ($request->query->getAlnum('rds') == "" && $request->query->getAlnum('famille') == "" && $request->query->getAlnum('recherche') == "")) {
            $data['rds'] = 1;
        } else {
            $data['rds'] = 0;
        }

        $listArticles = $em->getRepository('GRBackOfficeBundle:Article')->getQueryArticles($data);

        /**
         * @var $paginator \Knp\Component\Pager\Paginator
         */
        $paginator = $this->get('knp_paginator');
        $result = $paginator->paginate(
                $listArticles, $request->query->getInt('page', 1), $request->query->getInt('limit', 5)
        );

        $listFamilles = $em->getRepository('GRBackOfficeBundle:Famille')->findAll();

        return $this->render('GRBackOfficeBundle:Article:liste_articles.html.twig', array(
                    'listArticles' => $result,
                    'listFamilles' => $listFamilles,
                    'data' => $data
        ));
    }

Repository :

public function getQueryArticles($data) {
        $query = $this->createQueryBuilder('a')
                ->leftJoin('a.images', 'i')
                ->addSelect('i')
                ->leftJoin('a.type_stockage', 't')
                ->addSelect('t')
                ->leftJoin('a.famille', 'f')
                ->addSelect('f');
        if (array_key_exists('famille', $data)) {
            $query->andWhere('f.id = :famille')
                    ->setParameter('famille', $data['famille']);
        }
        if (array_key_exists('rds', $data)) {
            if ($data['rds'] == 0) {
                $query->andWhere('a.stock_actuel > 0');
            }
        }
        if (array_key_exists('recherche', $data)) {
            $query->andWhere('a.ref_article LIKE :recherche OR a.ref_logistique LIKE :recherche OR a.libelle LIKE :recherche')
                    ->setParameter('recherche', '%' . $data['recherche'] . '%');
        }
        $query->leftJoin('a.sousfamille', 's')
                ->addSelect('s')
                ->leftJoin('a.client', 'c')
                ->addSelect('c')
                ->andWhere('c.id = :client')
                ->setParameter('client', $data['clientId'])
                ->orderBy('a.ref_article', 'ASC')
                ->getQuery();


        return $query;
    }

When I use a space or an underscore in my "recherche" search filter, my table appears empty, and it seems to delete the spaces or the underscore so if I try with "John Doe" or "John_Doe", it will return me the results for "JohnDoe", which is empty.

If someone have an idea of how I can proceed, it will be appreciated !

1 Answer 1

1

You can use urlencode on your data.recherche. But there also more natural way to do this in twig

Sign up to request clarification or add additional context in comments.

2 Comments

I tried to change my input like this : <input name="recherche" style="width:100%" type="text" placeholder="Recherche" {% if data.recherche is defined %} value="{{ data.recherche|url_encode }}" {% endif %}> but it didn't change anyhing
Are parameters in the URL escaped when you send your form? Problem also can be related to getAlnum method because according to doc it doesn't return spaces and underscores (only numbers and chars). You can try to use just get instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.