src/Form/SearchFormType.php line 14

  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Product;
  4. use App\Repository\BrandRepository;
  5. use App\Repository\ModelRepository;
  6. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  7. use Symfony\Component\Form\Extension\Core\Type\TextType;
  8. use Symfony\Component\Form\AbstractType;
  9. use Symfony\Component\Form\FormBuilderInterface;
  10. use Symfony\Component\OptionsResolver\OptionsResolver;
  11. class SearchFormType extends AbstractType
  12. {
  13.     private BrandRepository $brandRepository;
  14.     private ModelRepository $modelRepository;
  15.     public function __construct(BrandRepository $brandRepositoryModelRepository $modelRepository)
  16.     {
  17.         $this->brandRepository $brandRepository;
  18.         $this->modelRepository $modelRepository;
  19.     }
  20.     public function buildForm(FormBuilderInterface $builder, array $options): void
  21.     {
  22.         $builder
  23.             ->add('brand'ChoiceType::class,[
  24.                 'required' => false,
  25.                 'placeholder' => false,
  26.                 'choices' => $this->getBrandChoices(),
  27.                 'data' => 0,
  28.             ])
  29.             ->add('product_model'ChoiceType::class,[
  30.                 'required' => false,
  31.                 'placeholder' => false,
  32.                 'choices' => $this->getModelChoices(),
  33.                 'data' => 0,
  34.             ])
  35.             ->add('search'TextType::class,[
  36.                 'required' => false,
  37.             ])
  38.         ;
  39.     }
  40.     public function configureOptions(OptionsResolver $resolver): void
  41.     {
  42.         $resolver->setDefaults([]);
  43.     }
  44.     private function getBrandChoices(): array
  45.     {
  46.         $brands $this->brandRepository->findAll();
  47.         $choices = [
  48.             'Toutes' => 0
  49.         ];
  50.         // Add the brand choices
  51.         foreach ($brands as $brand) {
  52.             $choices[$brand->getBrandName()] =$brand->getId() ;
  53.         }
  54.         return $choices;
  55.     }
  56.     private function getModelChoices(): array
  57.     {
  58.         $models $this->modelRepository->findAll();
  59.         $choices = [
  60.             'Tous' => 0
  61.         ];
  62.         // Add the brand choices
  63.         foreach ($models as $model) {
  64.             $choices[$model->getModelName()] =$model->getId() ;
  65.         }
  66.         return $choices;
  67.     }
  68. }