src/Form/SearchForm.php line 50

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Data\SearchData;
  4. use App\Entity\Category;
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\FormBuilderInterface;
  7. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\OptionsResolver\OptionsResolver;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. use Symfony\Component\Form\Extension\Core\Type\TextType;
  12. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  13. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  14. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  15. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  16. class SearchForm extends AbstractType
  17. {
  18.     private $translator;
  19.     private $requestStack;
  20.     private $session;
  21.     public function __construct(TranslatorInterface $translator,RequestStack $requestStack)
  22.     {
  23.         $this->setTranslator($translator);
  24.         $this->requestStack $requestStack;
  25.         
  26.     }
  27.     public function buildForm(FormBuilderInterface $builder, array $options): void
  28.     {
  29.         // $translator TranslatorInterface;
  30.         // todo translation dans le controller 
  31.         // $request = $this->requestStack->getCurrentRequest();
  32.         // $locale = $request->getLocale();
  33.         $this->session $this->getRequestStack()->getSession();
  34.         $locale $this->getSession()->get('_locale''en');
  35.         // dd($locale);
  36.         $rechercherlabel $this->getTranslator()->trans('rechercher'$locale);
  37.         $catlabel $this->getTranslator()->trans('category'$locale);
  38.         $prixminlabel $this->getTranslator()->trans('prixmini'$locale);
  39.         $prixmaxlabel $this->getTranslator()->trans('prixmax'$locale);
  40.         $enpromolabel $this->getTranslator()->trans('enpromo'$locale);
  41.         $rechercherlabel $this->getTranslator()->trans('rechercher'$locale);
  42.         $builder
  43.             ->add('q',TextType::class,[ 
  44.                 'label' => false,
  45.                 'required' => false,
  46.                 'attr' => [
  47.                     'placeholder' => $rechercherlabel
  48.                     'class' => 'form-control',
  49.                 ]
  50.                 ])
  51.                 ->add('categories',EntityType::class, [ 
  52.                 'label' => false,
  53.                 'required' => false,
  54.                 'class' => Category::class,
  55.                 'expanded' =>false,
  56.                 'mapped' => false,
  57.                 'choice_label' => 'nom',
  58.                 'placeholder' => $catlabel,
  59.                 'attr' => ['class' => 'form-control']
  60.             ])
  61.             //pour image 'constraints' => new File([
  62.                 // 'maxSize' => '1024K',
  63.                 // mimeType
  64.             // ]
  65.             
  66.             ->add('min',NumberType::class, [ 
  67.                 'label' => false,
  68.                 'required' => false,
  69.                 'attr' => [
  70.                     'placeholder' => $prixminlabel,
  71.                     'class' => 'form-control prix-width'
  72.                     
  73.                     ]
  74.             ])
  75.             ->add('max',NumberType::class, [ 
  76.                 'label' => false,
  77.                 'required' => false,
  78.                 'attr' => [
  79.                     'placeholder' => $prixmaxlabel,
  80.                     'class' => 'form-control prix-width'
  81.                     ]
  82.                     ])
  83.                     
  84.                     ->add('promo'CheckboxType::class, [
  85.                         'label' => $enpromolabel,
  86.                         'required' => false,
  87.                         'mapped' => false,
  88.                         'attr' => [
  89.                                 'class' => 'checkbox-wrapper '
  90.                                 ]
  91.                         ])
  92.                         
  93.                         ->add('submit'SubmitType::class, [
  94.                             'label' => $rechercherlabel,
  95.                             
  96.                             'attr' => [
  97.                                 'class' => 'btn btn-default btn-light '
  98.                                 ]
  99.                 // 'label' => 'Search',
  100.                         ])
  101.             
  102.         ;
  103.         // add submit button
  104.     }
  105.     public function configureOptions(OptionsResolver $resolver): void
  106.     {
  107.         $resolver->setDefaults([
  108.             'data_class' => SearchData::class,
  109.             'method' => 'GET',
  110.             'csrf_protection' => false
  111.         ]);
  112.     }
  113.     // rendre mon url plus propre 
  114.     public function getBlockPrefix(){
  115.         return '';
  116.     }
  117.     /**
  118.      * Get the value of translator
  119.      */ 
  120.     public function getTranslator()
  121.     {
  122.         return $this->translator;
  123.     }
  124.     /**
  125.      * Set the value of translator
  126.      *
  127.      * @return  self
  128.      */ 
  129.     public function setTranslator($translator)
  130.     {
  131.         $this->translator $translator;
  132.         return $this;
  133.     }
  134.     /**
  135.      * Get the value of session
  136.      */ 
  137.     public function getSession()
  138.     {
  139.         return $this->session;
  140.     }
  141.     /**
  142.      * Get the value of requestStack
  143.      */ 
  144.     public function getRequestStack()
  145.     {
  146.         return $this->requestStack;
  147.     }
  148.     /**
  149.      * Set the value of requestStack
  150.      *
  151.      * @return  self
  152.      */ 
  153.     public function setRequestStack($requestStack)
  154.     {
  155.         $this->requestStack $requestStack;
  156.         return $this;
  157.     }
  158. }