src/Form/BookingType.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Booking;
  4. use Symfony\Component\Form\AbstractType;
  5. use App\Repository\BookingConstraintRepository;
  6. use Symfony\Component\Form\FormBuilderInterface;
  7. use Symfony\Component\Validator\Constraints\Choice;
  8. use Symfony\Component\OptionsResolver\OptionsResolver;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. use Symfony\Component\Form\Extension\Core\Type\DateType;
  11. use Symfony\Component\Form\Extension\Core\Type\TelType;
  12. use Symfony\Component\Form\Extension\Core\Type\TextType;
  13. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  14. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  15. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  16. use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  17. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  18. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  19. use Symfony\Component\Validator\Constraints\LessThanOrEqual;
  20. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  21. class BookingType extends AbstractType
  22. {
  23.     private $constraintRepo;
  24.     private $session;
  25.     private $locale;
  26.     private $translator;
  27.     public function __construct(TranslatorInterface $translatorBookingConstraintRepository $constraintRepoSessionInterface $session)
  28.     {
  29.         $this->constraintRepo $constraintRepo;
  30.         $this->session $session;
  31.         $this->locale $session->get('_locale');
  32.         $this->translator $translator;
  33.     }
  34.     public function buildForm(FormBuilderInterface $builder, array $options): void
  35.     {
  36.         $builder
  37.             ->add('firstName'TextType::class, [
  38.                 'label' => $this->translator->trans("Prénom"),
  39.                 'required' => true,
  40.                 'attr' => [
  41.                     'placeholder' => $this->translator->trans("Votre prénom"),
  42.                 ]
  43.             ])
  44.             ->add('lastName'TextType::class, [
  45.                 'label' => $this->translator->trans("Nom"),
  46.                 'required' => true,
  47.                 'attr' => [
  48.                     'placeholder' => $this->translator->trans("Votre nom"),
  49.                 ]
  50.             ])
  51.             ->add('email'EmailType::class, [
  52.                 'label' => $this->translator->trans("Email"),
  53.                 'required' => true,
  54.                 'attr' => [
  55.                     'placeholder' => "exemple@mail.fr",
  56.                 ]
  57.             ])
  58.             ->add('phone'TelType::class, [
  59.                 'label' => $this->translator->trans("Téléphone"),
  60.                 'required' => true,
  61.                 'attr' => [
  62.                     'placeholder' => "ex : 06 07 08 09 10",
  63.                 ]
  64.             ])
  65.             ->add('gender'ChoiceType::class, [
  66.                 'choices' => [
  67.                     'M.' => 'M.',
  68.                     'Mme' => 'Mme',
  69.                     'Autre' => 'Autre',
  70.                 ],
  71.                 'label' => $this->translator->trans("Civilité"),
  72.                 'required' => true,
  73.                 'constraints' => [new Choice(['M.''Mme''Autre'])],
  74.             ])
  75.             ->add('approval'CheckboxType::class, [
  76.                 'label' => $this->translator->trans("Je m'engage à régler le solde."),
  77.                 'required' => true
  78.             ])
  79.             ->add('rgpd'CheckboxType::class, [
  80.                 'label' => $this->translator->trans("J'accepte que les données renseignées soient conservées conformément à la politique de confidentialité."),
  81.                 'required' => true
  82.             ])
  83.             ->add('paymentMethod'ChoiceType::class, [
  84.                 'choices' => $this->getPaymentMethods(),
  85.                 'label' => $this->translator->trans("Moyen de paiement"),
  86.                 'required' => true
  87.             ])
  88.             ->add('comment'TextareaType::class, [
  89.                 'label' => $this->translator->trans("Commentaire"),
  90.                 'required' => false,
  91.                 'attr' => [
  92.                     'placeholder' => $this->translator->trans("Précisions sur vos attentes, questions, ..."),
  93.                 ]
  94.             ])
  95.             ->add('number'IntegerType::class, [
  96.                 'label' => $this->translator->trans("Nombre de personnes (7 maximum)"),
  97.                 'required' => true,
  98.                 'constraints' => new LessThanOrEqual($this->getMaxNumber()),
  99.                 'attr' => [
  100.                     'min' => 1,
  101.                     'max' => $this->getMaxNumber(),
  102.                     'step' => 1,
  103.                     'placeholder' => "1 - 7",
  104.                 ]
  105.             ])
  106.             ->add('children'IntegerType::class, [
  107.                 'label' => $this->translator->trans("Dont enfant(s)"),
  108.                 'required' => true,
  109.                 'constraints' => new LessThanOrEqual($this->getMaxNumber() - 1),
  110.                 'attr' => [
  111.                     'min' => 1,
  112.                     'max' => $this->getMaxNumber() - 1,
  113.                     'step' => 1,
  114.                     'placeholder' => "1 - 6",
  115.                 ]
  116.             ])
  117.             ->add('startDate'DateType::class, [
  118.                 'label' => $this->translator->trans("Date d'arrivé"),
  119.                 'required' => true,
  120.                 'widget' => 'single_text',
  121.                 'html5' => false,
  122.                 'format' => 'dd/MM/yyyy',
  123.                 'attr' => [
  124.                     'placeholder' => $this->translator->trans("Selectionnez une date"),
  125.                     'class' => 'wrap-box',
  126.                 ]
  127.             ])
  128.             ->add('endDate'DateType::class, [
  129.                 'label' => $this->translator->trans("Date de départ"),
  130.                 'required' => true,
  131.                 'widget' => 'single_text',
  132.                 'html5' => false,
  133.                 'format' => 'dd/MM/yyyy',
  134.                 'attr' => [
  135.                     'placeholder' => $this->translator->trans("Selectionnez une date"),
  136.                     'class' => 'wrap-box',
  137.                 ]
  138.             ])
  139.             ->add('amount'HiddenType::class, [
  140.                 'label' => $this->translator->trans("Prix"),
  141.                 'required' => true,
  142.             ])
  143.         ;
  144.     }
  145.     private function getMaxNumber()
  146.     {
  147.         return $this->constraintRepo->findOneBy([])->getMaxNumber();
  148.     }
  149.     private function getPaymentMethods()
  150.     {
  151.         $paymentMethods $this->constraintRepo->findOneBy([])->getPaymentMethods();
  152.         $temp = [];
  153.         foreach ($paymentMethods as $key => $item) {
  154.             $temp[$item->translate($this->locale)->getTitle()] = $item->translate($this->locale)->getTitle();
  155.         }
  156.         return $temp;
  157.     }
  158.     public function configureOptions(OptionsResolver $resolver): void
  159.     {
  160.         $resolver->setDefaults([
  161.             'data_class' => Booking::class,
  162.         ]);
  163.     }
  164. }