Reputation: 85
I'm trying to create a search form where the user can search for an event by selecting its city and category. So I would like to create two selections "city" and "category" with the options already saved in the database.
More information :
For later :
I'll save it for later... but I'd also like to create a country selection. The "Country" entity is created and it is connected by a OneToMany to the "City" entity. And if the city is selected without its country, the country would be defined automatically.
EDIT :
EventSearchType.php
<?php
namespace App\Form;
use App\Entity\Events;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class SearchType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('bigcity', EntityType::class, [
'class' => Location::class,
'choice_label' => 'bigcity',
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => null
]);
}
}
events.html.twig
{% extends 'base.html.twig' %}
{% block title %}Liste des sorties et des activités{% endblock %}
{% block main %}
<div class="page position-relative">
<div class="form container position-absolute top-50 start-50 translate-middle">
{{ form(form) }}
</div>
</div>
{% endblock %}
EventsController.php
<?php
namespace App\Controller;
use App\Entity\Events;
use App\Entity\BigCity;
use App\Entity\Country;
use App\Entity\Categories;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Form\Extension\Core\Type\SearchType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class EventsController extends AbstractController
{
#[Route('/search', name: 'search')]
public function search(Request $request)
{
$form = $this->createForm(SearchType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
return $this->render('front/events.html.twig', $data);
}
return $this->render('front/search.html.twig', [
'form' => $form->createView()
]);
}
}
Upvotes: 0
Views: 360
Reputation: 85
This was my mistake : App\Form\SearchType instead of Symfony\Component\Form\Extension\Core\Type\SearchType in EventsController.php file.
It's working properly now. :)
Upvotes: 1