src/Form/RegistrationFormType.php line 26

  1. <?php
  2. namespace App\Form;
  3. use App\Entity\AppConstants;
  4. use App\Entity\Inscription;
  5. use App\Entity\User;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Doctrine\ORM\QueryBuilder;
  8. use Symfony\Component\Form\AbstractType;
  9. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  10. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  11. use Symfony\Component\Form\Extension\Core\Type\CountryType;
  12. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  13. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  14. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  15. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  16. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  17. use Symfony\Component\Form\Extension\Core\Type\TextType;
  18. use Symfony\Component\Form\FormBuilderInterface;
  19. use Symfony\Component\Form\FormInterface;
  20. use Symfony\Component\OptionsResolver\OptionsResolver;
  21. use Symfony\Component\Validator\Constraints as Assert;
  22. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  23. class RegistrationFormType extends AbstractType
  24. {
  25.     public function __construct(private EntityManagerInterface $em)
  26.     {
  27.         // parent::__construct();
  28.     }
  29.     public function buildForm(FormBuilderInterface $builder, array $options): void
  30.     {
  31.         $builder
  32.             ->add('username')
  33.             ->add('firstname'TextType::class, [
  34.                 'required' => true,
  35.                 'constraints' => [
  36.                     new Assert\NotBlank(),
  37.                 ]
  38.             ])
  39.             ->add('lastname'TextType::class, [
  40.                 'required' => true,
  41.                 'constraints' => [
  42.                     new Assert\NotBlank(),
  43.                 ]
  44.             ])
  45.             ->add('title'ChoiceType::class, [
  46.                 'required' => true,
  47.                 'choices' => ['Choose your title' => "",] + array_combine(AppConstants::TITLE_LISTAppConstants::TITLE_LIST),
  48.                 'constraints' => [
  49.                     new Assert\NotBlank(),
  50.                 ]
  51.             ])
  52.             ->add('email'EmailType::class, [
  53.                 'required' => true,
  54.                 'constraints' => [
  55.                     new Assert\NotBlank(),
  56.                     new Assert\Email(),
  57.                     new Assert\Length([
  58.                         'min' => 5,
  59.                     ]),
  60.                     new Assert\Callback(function ($payloadExecutionContextInterface $context) use ($options) {
  61.                         /** @var Inscription $data */
  62.                         $data $context->getRoot()->getData();
  63.                         /** @var Inscription $inscription */
  64.                         /** @var QueryBuilder $qb */
  65.                         $qb $this->em->getRepository(Inscription::class)->createQueryBuilder('p')
  66.                             ->join('p.evenement''e')
  67.                             ->where('p.mail = :mail')
  68.                             ->setParameter('mail'$payload)
  69.                             // ->orderBy('p.arg', 'ASC')
  70.                             ->setMaxResults(1);
  71.                         $qb->andWhere('p.status = :status')
  72.                             ->setParameter('status'Inscription::STATUS_VALID);
  73.                         if (!empty($options['evenement_id'])) {
  74.                             $qb->andWhere('e.id = :evenement_id')
  75.                                 ->setParameter('evenement_id'$options['evenement_id']);
  76.                             if (!empty($options['page']) && $options['page'] == 'edit' && $data && $inscription_id $data->getId()) {
  77.                                 $qb->andWhere('p.id != :id')
  78.                                     ->setParameter('id'$data->getId());
  79.                             }
  80.                             $inscription $qb->getQuery()->getOneOrNullResult();
  81.                             if ($inscription) {
  82.                                 $context->addViolation('Cette adresse e-mail est déjà utilisée par un autre participant', []); // utilisateur
  83.                             }
  84.                         }
  85.                         if (function_exists('dump')) :
  86.                             // dump(@compact('data', 'payload', 'inscription', 'context', 'inscription_id', 'inscription', 'options'));
  87.                         endif;
  88.                     }),
  89.                 ]
  90.             ])
  91.             ->add('sexe'ChoiceType::class, [
  92.                 'required' => true,
  93.                 'choices'  => [
  94.                     'Choose your sexe' => "",
  95.                     'Homme' => Inscription::SEXE_HOMME,
  96.                     'Femme' => Inscription::SEXE_FEMME,
  97.                 ],
  98.                 'constraints' => [
  99.                     new Assert\NotBlank(),
  100.                     new Assert\Choice(choicesAppConstants::SEXE_LIST),
  101.                 ]
  102.             ])
  103.             ->add('speciality'ChoiceType::class, [
  104.                 'required' => true,
  105.                 'choices' => ['Choose your speciality' => "",] + array_combine(AppConstants::SPECIALITY_LISTAppConstants::SPECIALITY_LIST),
  106.                 'constraints' => [
  107.                     new Assert\NotBlank(),
  108.                     new Assert\Choice(choicesAppConstants::SPECIALITY_LIST),
  109.                 ]
  110.             ])
  111.             ->add('activity'ChoiceType::class, [
  112.                 'required' => true,
  113.                 'choices' => ['Choose your activity' => "",] + array_combine(AppConstants::ACTIVITY_LISTAppConstants::ACTIVITY_LIST),
  114.                 'constraints' => [
  115.                     new Assert\NotBlank(),
  116.                     new Assert\Choice(choicesAppConstants::ACTIVITY_LIST),
  117.                 ]
  118.             ])
  119.             ->add('mobile'TextType::class, [
  120.                 'required' => true,
  121.                 'constraints' => [
  122.                     new Assert\NotBlank(),
  123.                     new Assert\Length([
  124.                         'min' => 8,
  125.                         'minMessage' => 'Le numéro de télephone doit contenir au minimum {{ limit }} chiffres',
  126.                     ])
  127.                 ]
  128.             ])
  129.             ->add('country'CountryType::class, [
  130.                 'required' => true,
  131.                 'preferred_choices' => [
  132.                     'TN',
  133.                     'FR',
  134.                     'DZ',
  135.                     'MA',
  136.                 ],
  137.                 'constraints' => [
  138.                     new Assert\NotBlank(),
  139.                 ]
  140.             ])
  141.             ->add('adresse'TextType::class, [
  142.                 'required' => true,
  143.                 'constraints' => [
  144.                     new Assert\NotBlank(),
  145.                 ]
  146.             ])
  147.             ->add('city'TextType::class, [
  148.                 'required' => true,
  149.                 'constraints' => [
  150.                     new Assert\NotBlank(),
  151.                 ]
  152.             ])
  153.             ->add('postalCode'TextType::class, [
  154.                 'required' => true,
  155.                 'constraints' => [
  156.                     new Assert\NotBlank(),
  157.                 ]
  158.             ])
  159.             ->add('workplace'TextType::class, [
  160.                 'required' => true,
  161.                 'constraints' => [
  162.                     new Assert\NotBlank(),
  163.                 ]
  164.             ])
  165.             ->add('password'RepeatedType::class,
  166.                 [
  167.                     'type' => PasswordType::class,
  168.                     'mapped'=>false,
  169.                     'invalid_message'=> 'Please check password',
  170.                     'label'=> 'Password',
  171.                     'required'=> true,
  172.                     'first_options'=>  ['label'=> 'Tapez un mot de passe'],
  173.                     'second_options'=>  ['label'=> 'Retapez votre mot de passe'  ]
  174.                 ]
  175.             )
  176.             // ->add('demandeMembership', HiddenType::class, [
  177.             //     'data' => 0,
  178.             // ])
  179.             ->add('demandeMembership'ChoiceType::class, [
  180.                 'label' => false,
  181.                 'required' => false,
  182.                 'expanded' => true,
  183.                 'multiple' => false,
  184.                 'choices'=>[
  185.                     'Become a member of the STP'=>1
  186.                 ],
  187.                 'placeholder'=>false,
  188.             ])
  189.             ->add('save'SubmitType::class, ['label' => 'Submit','attr'=>['class'=>'btn btn-primary']]);
  190.     }
  191.     public function configureOptions(OptionsResolver $resolver): void
  192.     {
  193.         $resolver->setDefaults([
  194.             'data_class' => User::class,
  195.         ]);
  196.     }
  197. }