src/Form/User/UserRegisterType.php line 166

Open in your IDE?
  1. <?php
  2. namespace App\Form\User;
  3. use App\Form\User\AddInformationType;
  4. use App\Manager\User\ReferentManager;
  5. use App\Model\User\Referent;
  6. use App\Model\User\User;
  7. use Symfony\Component\Form\AbstractType;
  8. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  9. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  10. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  11. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  12. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  13. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  14. use Symfony\Component\Form\Extension\Core\Type\TextType;
  15. use Symfony\Component\Form\FormBuilderInterface;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. use Symfony\Component\OptionsResolver\OptionsResolver;
  18. use Symfony\Component\Validator\Constraints as Assert;
  19. use Symfony\Component\Validator\Constraints\NotNull;
  20. class UserRegisterType extends AbstractType
  21. {
  22.     private ReferentManager $referentManager;
  23.     private RequestStack $requestStack;
  24.     public function __construct(ReferentManager $referentManagerRequestStack $requestStack)
  25.     {
  26.         $this->referentManager $referentManager;
  27.         $this->requestStack $requestStack;
  28.     }
  29.     public function buildForm(FormBuilderInterface $builder, array $options)
  30.     {
  31.         $builder
  32.             ->add('companyName'TextType::class, [
  33.                 'label' => 'form.register.company_name',
  34.                 'required' => true,
  35.                 'translation_domain' => 'form',
  36.                 'constraints' => [
  37.                     new Assert\NotBlank(['message' => 'La raison sociale est obligatoire.']),
  38.                     new Assert\Length([
  39.                         'max' => 50,
  40.                         'maxMessage' => 'La raison sociale ne peut pas dépasser {{ limit }} caractères.',
  41.                     ]),
  42.                 ],
  43.             ])
  44.             ->add('email'RepeatedType::class, [
  45.                 'type' => EmailType::class,
  46.                 'invalid_message' => 'register.email.match',
  47.                 'translation_domain' => 'form',
  48.                 'first_options' => [
  49.                     'label' => 'form.register.email.first',
  50.                     'translation_domain' => 'form',
  51.                     'constraints' => [
  52.                         new Assert\NotBlank(['message' => 'Votre adresse email est obligatoire.']),
  53.                     ],
  54.                 ],
  55.                 'second_options' => [
  56.                     'label' => 'form.register.email.second',
  57.                     'translation_domain' => 'form',
  58.                     'constraints' => [
  59.                         new Assert\NotBlank(['message' => 'La confirmation de votre adressse email est obligatoire.']),
  60.                     ],
  61.                 ],
  62.             ])
  63.             ->add('plainPassword'RepeatedType::class, [
  64.                 'type' => PasswordType::class,
  65.                 'invalid_message' => 'register.password.match',
  66.                 'translation_domain' => 'form',
  67.                 'first_options' => [
  68.                     'label' => 'form.register.password.first',
  69.                     'translation_domain' => 'form',
  70.                     'constraints' => [
  71.                         new Assert\NotBlank(['message' => 'Votre mot de passe est obligatoire.']),
  72.                     ],
  73.                 ],
  74.                 'second_options' => [
  75.                     'label' => 'form.register.password.second',
  76.                     'translation_domain' => 'form',
  77.                     'constraints' => [
  78.                         new Assert\NotBlank(['message' => 'La confirmation de votre mot de passe est obligatoire.']),
  79.                     ],
  80.                 ],
  81.             ])
  82.             ->add('phoneNumber'TextType::class, [
  83.                 'label' => 'form.register.number_phone',
  84.                 'translation_domain' => 'form',
  85.                 'constraints' => [
  86.                     new Assert\Regex(['pattern' => '/^((\+)33|0)[1-9](\d{2}){4}$/','message' => 'Le numéro de téléphone est incorrect']),
  87.                     new Assert\NotBlank(['message' => 'Le numéro de téléphone est obligatoire.']),
  88.                 ]
  89.             ])
  90.             ->add('optinSales'ChoiceType::class, [
  91.                 'label' => 'form.register.optin.sales',
  92.                 'translation_domain' => 'form',
  93.                 'choices'  => [
  94.                     'Oui' => true,
  95.                     'Non' => false,
  96.                 ],
  97.                 'expanded' => true,
  98.                 'multiple' => false,
  99.                 'required' => true,
  100.                 'constraints' => [
  101.                     new NotNull(['message' => 'Veuillez sélectionner une option.']),
  102.                 ],
  103.             ])
  104.             ->add('hasAdditionalInformation'CheckboxType::class, [
  105.                 'label' => 'Veuillez cocher cette case si vous commandez déjà chez un grossiste',
  106.                 'translation_domain' => 'form',
  107.             ])
  108.             ->add('additionalInformation'CollectionType::class, [
  109.                 'entry_type' => addInformationType::class,
  110.                 'allow_add' => true,
  111.                 'allow_delete' => true,
  112.                 'entry_options' => ['label' => false],
  113.                 'label' => false,
  114.                 'translation_domain' => 'form',
  115.             ])
  116.             ->add('optinBrand'CheckboxType::class, [
  117.                 'label' => 'form.register.optin.brand',
  118.                 'translation_domain' => 'form',
  119.             ])
  120.             ->add('optinPhone'CheckboxType::class, [
  121.                 'label' => 'form.register.optin.phone',
  122.                 'translation_domain' => 'form',
  123.             ])
  124.             ->add('optinProcess'CheckboxType::class, [
  125.                 'label' => 'form.register.optin.process',
  126.                 'translation_domain' => 'form',
  127.                 'constraints' => [
  128.                     new Assert\NotBlank([
  129.                         'message' => 'register.optin.process.required',
  130.                     ]),
  131.                 ],
  132.             ])
  133.         ;
  134.         $referents $this->referentManager->getReferents();
  135.         if (!empty($referents)) {
  136.             $builder
  137.                 ->add('referent'ChoiceType::class, [
  138.                     'choices' => $referents,
  139.                     'multiple' => false,
  140.                     'expanded' => false,
  141.                     'label' => 'form.register.referent',
  142.                     'translation_domain' => 'form',
  143.                     'placeholder' => 'Sélectionnez votre activité',
  144.                     'constraints' => [
  145.                         new Assert\NotBlank([
  146.                             'message' => 'register.referent.required',
  147.                         ]),
  148.                     ],
  149.                     'choice_value' => function(?Referent $referent) {
  150.                         return $referent '/api/referents/' $referent->getId() : '';
  151.                     },
  152.                     'choice_label' => function(Referent $referent) {
  153.                         return $referent->getCompanyName();
  154.                     },
  155.                 ]);
  156.         }
  157.         $request $this->requestStack;
  158.         $session $this->requestStack->getSession();
  159.         $response = isset($request->getMasterRequest()->request->all()['user_register']['hasAdditionalInformation']) ? $request->getMasterRequest()->request->all()['user_register']['hasAdditionalInformation'] : false;
  160.         $session->set('hasAdditionalInformation',$response);
  161.     }
  162.     public function configureOptions(OptionsResolver $resolver)
  163.     {
  164.         $resolver->setDefaults([
  165.             'data_class' => User::class,
  166.         ]);
  167.     }
  168. }