TP7 Formulaires de Recherche
TP7 Formulaires de Recherche
TP7 Formulaires de Recherche
On se propose dans cet atelier de créer des formulaires de recherche des articles :
Recherche des articles par Nom
Recherche des articles par Catégorie
Recherche des articles dont le prix est compris entre deux valeurs
<?php
namespace App\Entity;
class PropertySearch
{
private $nom;
return $this;
}
}
{% extends 'base.html.twig' %}
{% block title%} Liste des Articles{% endblock %}
{% block body %}
{{ form_start(form) }}
<div class="col">
<div class="form-group">
<button type="submit" class="btn btn-success">Rechercher</button>
</div>
</div>
</div>
{{ form_end(form) }
{% if articles % }
…
/**
*@Route("/",name="article_list")
*/
public function home(Request $request)
{
$propertySearch = new PropertySearch();
$form = $this->createForm(PropertySearchType::class,$propertySearch);
$form->handleRequest($request);
//initialement le tableau des articles est vide,
//c.a.d on affiche les articles que lorsque l'utilisateur
//clique sur le bouton rechercher
$articles= [];
$nom = $propertySearch->getNom();
if ($nom!="")
//si on a fourni un nom d'article on affiche tous les articles ayant ce n
om
$articles= $this->getDoctrine()->getRepository(Article::class)-
>findBy(['nom' => $nom] );
else
//si si aucun nom n'est fourni on affiche tous les articles
$articles= $this->getDoctrine()->getRepository(Article::class)-
>findAll();
}
return $this->render('articles/index.html.twig',[ 'form' =>$form-
>createView(), 'articles' => $articles]);
}
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
class CategorySearch
{
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Category")
*/
private $category;
return $this;
}
}
<?php
namespace App\Form;
use App\Entity\CategorySearch;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use App\Entity\Category;
use App\Entity\CategorySearch;
use App\Form\CategorySearchType;
/**
* @Route("/art_cat/", name="article_par_cat")
* Method({"GET", "POST"})
*/
public function articlesParCategorie(Request $request) {
$categorySearch = new CategorySearch();
$form = $this->createForm(CategorySearchType::class,$categorySearch);
$form->handleRequest($request);
$articles= [];
if ($category!="")
$articles= $category->getArticles();
else
$articles= $this->getDoctrine()->getRepository(Article::class)->findAll();
}
return $this-
>render('articles/articlesParCategorie.html.twig',['form' => $form-
>createView(),'articles' => $articles]);
}
{% extends 'base.html.twig' %}
{% block title%} Liste des Articles par Catégorie{% endblock %}
{% block body %}
{{ form_start(form) }}
<div class="col">
<div class="form-group">
<button type="submit" class="btn btn-success">Rechercher</button>
</div>
</div>
</div>
{{ form_end(form) }}
{% if articles %}
<table id="articles" class="table table-striped">
<thead>
<tr>
<th>Nom</th>
<th>Prix</th>
<th>Catégorie</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for article in articles %}
<tr>
<td>{{ article.nom }}</td>
<td>{{ article.prix }}</td>
<td>{{ article.category.titre }}</td>
<td>
<a href="/article/{{ article.id }}" class="btn btn-
dark">Détails</a>
<a href="/article/edit/{{ article.id }}" class="btn btn-
dark">Modifier</a>
<a href="/article/delete/{{ article.id }}" class="btn btn-
danger"
onclick="return confirm('Etes-
vous sûr de supprimer cet article?');">Supprimer</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>Aucun articles</p>
{% endif %}
{% endblock %}
Recherche des articles dont le prix est compris entre deux valeurs
<?php
namespace App\Entity;
class PriceSearch
{
/**
* @var int|null
*/
private $minPrice;
/**
* @var int|null
*/
private $maxPrice;
return $this;
}
}
<?php
namespace App\Form;
use App\Entity\PriceSearch;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
->orderBy('a.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
/**
* @Route("/art_prix/", name="article_par_prix")
* Method({"GET"})
*/
public function articlesParPrix(Request $request)
{
$articles= [];
$articles= $this->getDoctrine()->
getRepository(Article::class)->findByPriceRange($minPrice,$maxPrice);
}
{% extends 'base.html.twig' %}
{% block title%} Liste des Articles par prix{% endblock %}
{% block body %}
{{ form_start(form) }}
<div class="form-row align-items-end" >
<div class="col">
{{ form_row(form.minPrice) }}
</div>
<div class="col">
{{ form_row(form.maxPrice) }}
</div>
<div class="col">
<div class="form-group">
<button type="submit" class="btn btn-success">Rechercher</button>
</div>
</div>
</div>
{{ form_end(form) }}
{% if articles %}
<table id="articles" class="table table-striped">
<thead>
<tr>
<th>Nom</th>
<th>Prix</th>
<th>Catégorie</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for article in articles %}
<tr>
<td>{{ article.nom }}</td>
<td>{{ article.prix }}</td>
<td>{{ article.category.titre }}</td>
<td>
<a href="/article/{{ article.id }}" class="btn btn-
dark">Détails</a>
<li class="nav-item">
<a href="{{path('article_par_prix')}}" class="nav-link">Recherche par prix</a>
</li>
18. Testez :