src/Form/AppointmentType.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Appointment;
  4. use App\Entity\AppointmentCategory;
  5. use App\Repository\ParametersRepository;
  6. use Symfony\Component\Form\AbstractType;
  7. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  8. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  9. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  10. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  11. use Symfony\Component\Form\Extension\Core\Type\TextType;
  12. use Symfony\Component\Form\FormBuilderInterface;
  13. use Symfony\Component\OptionsResolver\OptionsResolver;
  14. class AppointmentType extends AbstractType
  15. {
  16.     public function __constructParametersRepository $parametersRepository)
  17.     {
  18.         $this->params $parametersRepository->findOneBy([]);
  19.     }
  20.     public function buildForm(FormBuilderInterface $builder, array $options): void
  21.     {
  22.         $builder
  23.             ->add('lastName'TextType::class, [
  24.                 'label' => 'Votre nom de famille',
  25.             ])
  26.             ->add('firstName'TextType::class, [
  27.                 'label' => 'Votre prénom',
  28.             ])
  29.             ->add('email'EmailType::class, [
  30.                 'label' => 'Votre email',
  31.             ])
  32.             ->add('phone'TextType::class, [
  33.                 'label' => 'Votre téléphone (10 chiffres)',
  34.                 'required' => false,
  35.                 'attr' => [
  36.                     'pattern'=> '\d{10}$',
  37.                     'placeholder' => '0000000000',
  38.                     'maxlength' => 10
  39.                 ]
  40.             ])
  41.             ->add('reason'ChoiceType::class, [
  42.                 'label' => 'Raison de votre rendez-vous',
  43.                 'required' => true,
  44.                 'choices' => $this->getReasonsChoices(),
  45.                 'multiple' => false,
  46.                 'expanded' => false,
  47.             ])
  48.             ->add('inscriptionType'ChoiceType::class, [
  49.                 'label' => 'S\'agit il',
  50.                 'choices' => $this->makeCategoryChoices(),
  51.                 'expanded' => true,
  52.                 'multiple' => false,
  53.                 'empty_data' => 'NEW'
  54.             ])
  55.             ->add('childCount'NumberType::class, [
  56.                 'label' => 'Combien d\'enfants souhaitez vous inscrire ?',
  57.                 'required' => true,
  58.                 'html5' => true,
  59.                 'attr' => [
  60.                     'data-controller' => 'children',
  61.                     'data-action'     => 'change->children#addInputs',
  62.                     'class' => 'small-number'
  63.                 ],
  64.                 'empty_data' => 0
  65.             ])
  66.             ->add('slot'HiddenType::class, [
  67.                 'mapped' => false,
  68.             ])
  69.         ;
  70.     }
  71.     private function getReasonsChoices(): array
  72.     {
  73.         $choices = [];
  74.         $reasons $this->params->getAppointmentReasons();
  75.         if(!$reasons) {
  76.             return [
  77.                 'Inscriptions mercredis' => 'Inscriptions mercredis',
  78.                 'Inscriptions vacances' => 'Inscriptions vacances',
  79.             ];
  80.         }
  81.         foreach ($reasons as $reason) {
  82.             $choices[$reason] = $reason;
  83.         }
  84.         return $choices;
  85.     }
  86.     private function makeCategoryChoices()
  87.     {
  88.         $choices = [];
  89.         foreach (AppointmentCategory::CATEGORIES as $identifier => $label) {
  90.            $choices[$label] = $identifier;
  91.         }
  92.         return $choices;
  93.     }
  94.     public function configureOptions(OptionsResolver $resolver): void
  95.     {
  96.         $resolver->setDefaults([
  97.             'data_class' => Appointment::class,
  98.             'allow_extra_fields' => true,
  99.         ]);
  100.     }
  101. }