src/Form/SearchFormType.php line 124

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Gibier;
  4. use App\Repository\AnimalRepository;
  5. use App\Services\Utils;
  6. use Doctrine\ORM\EntityRepository;
  7. use Symfony\Component\Form\AbstractType;
  8. use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
  9. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  10. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  11. use Symfony\Component\Form\FormBuilderInterface;
  12. use Symfony\Component\Form\Extension\Core\Type\TextType;
  13. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  14. use Symfony\Component\Form\Extension\Core\Type\MoneyType;
  15. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  16. use App\Entity\HuntingMode;
  17. use App\Entity\Weapon;
  18. use App\Entity\Animal;
  19. use Symfony\Component\OptionsResolver\OptionsResolver;
  20. class SearchFormType extends AbstractType
  21. {
  22.     public function __construct(private Utils $utils)
  23.     {}
  24.     public function buildForm(FormBuilderInterface $builder, array $options): void
  25.     {
  26.         $builder
  27.             ->setMethod('GET')
  28.             ->add('region',ChoiceType::class,[
  29.                 'mapped' => true,
  30.                 'expanded' => true,
  31.                 'multiple' => true,
  32.                 'placeholder' => 'Régions',
  33.                 'choice_loader'  => new CallbackChoiceLoader(function() {
  34.                     return $this->utils->getRegionsToChoices();
  35.                 }),
  36.                 'required' => false,
  37.             ])
  38.             ->add('huntingMode'EntityType::class, array(
  39.                 'class' => HuntingMode::class,
  40.                 'query_builder' => function (EntityRepository $er) {
  41.                     return $er->createQueryBuilder('u')
  42.                         ->orderBy('u.title''ASC');
  43.                 },
  44.                 'choice_label' => 'title',
  45.                 'mapped' => true,
  46.                 'expanded' => true,
  47.                 'multiple' => true,
  48.                 'required' => false,
  49.             ))
  50.             ->add('animals'EntityType::class, array(
  51.                 'class' => Animal::class,
  52.                 'query_builder' => function (EntityRepository $er) {
  53.                     return $er->createQueryBuilder('a')
  54.                         ->orderBy('a.name''ASC');
  55.                 },
  56.                 'choice_label' => 'name',
  57.                 'mapped' => true,
  58.                 'expanded' => true,
  59.                 'multiple' => true,
  60.                 'required' => false,
  61.             ))
  62.             ->add('dates',TextType::class,[
  63.                 'required' => false,
  64.                 'empty_data' => null,
  65.                 'mapped' => true,
  66.                 'attr' => [
  67.                     'class' => 'search_form_dates'
  68.                 ]
  69.             ])
  70.             ->add('departement',ChoiceType::class,[
  71.                 'mapped' => true,
  72.                 'expanded' => true,
  73.                 'multiple' => true,
  74.                 'placeholder' => 'Département',
  75.                 'choice_loader'  => new CallbackChoiceLoader(function() {
  76.                     return $this->utils->getListToChocies();
  77.                 }),
  78.                 'required' => false,
  79.             ])
  80.             ->add('weapons'EntityType::class, array(
  81.                 'class' => Weapon::class,
  82.                 'query_builder' => function (EntityRepository $er) {
  83.                     return $er->createQueryBuilder('a')
  84.                         ->orderBy('a.name''ASC');
  85.                 },
  86.                 'choice_label' => 'name',
  87.                 'mapped' => true,
  88.                 'expanded' => true,
  89.                 'multiple' => true,
  90.                 'required' => false,
  91.             ))
  92.             ->add('priceMin',MoneyType::class,[
  93.                 'required' => false,
  94.                 'attr' => [
  95.                     'class' => 'price',
  96.                     'placeholder' => ''
  97.                 ]
  98.             ])
  99.             ->add('priceMax',MoneyType::class,[
  100.                 'required' => false,
  101.                 'attr' => [
  102.                     'class' => 'price',
  103.                     'placeholder' => ''
  104.                 ]
  105.             ])
  106.             ->add('type',ChoiceType::class,[
  107.                 'expanded' => true,
  108.                 'multiple' => true,
  109.                 'placeholder' => 'Type d\'offre',
  110.                 'choices'  => [
  111.                     'Action' => 'ac',
  112.                     'Bracelet' => 'br',
  113.                     'Journée' => 'jr',
  114.                 ],
  115.                 'required' => false,
  116.             ])
  117.             ->add('gibier'EntityType::class, array(
  118.                 'class' => Gibier::class,
  119.                 'query_builder' => function (EntityRepository $er) {
  120.                     return $er->createQueryBuilder('a')
  121.                         ->orderBy('a.title''ASC');
  122.                 },
  123.                 'choice_label' => 'title',
  124.                 'placeholder' => 'Type de gibier',
  125.                 'mapped' => true,
  126.                 'expanded' => true,
  127.                 'multiple' => true ,
  128.                 'required' => false,
  129.             ))
  130.             ->add('orderBy'HiddenType::class)
  131.             ->add('orderSort'HiddenType::class)
  132.             ->add('save'SubmitType::class, [
  133.                 'label' => 'Appliquer'
  134.             ])
  135.         ;
  136.     }
  137.     public function configureOptions(OptionsResolver $resolver): void
  138.     {
  139.         $resolver->setDefaults([
  140.             'isMobile' => false,
  141.         ]);
  142.     }
  143. }