src/Form/Type/Registration/RegistrationFormType.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\Form\Type\Registration;
  3. use App\Entity\User;
  4. use App\Model\Form\UserField;
  5. use App\Services\DTV\YamlConfig\YamlReader;
  6. use Exception;
  7. use Symfony\Component\Form\AbstractType;
  8. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  9. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  10. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  11. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  12. use Symfony\Component\Form\Extension\Core\Type\TextType;
  13. use Symfony\Component\Form\FormBuilderInterface;
  14. use Symfony\Component\OptionsResolver\OptionsResolver;
  15. use Symfony\Component\Validator\Constraints\IsTrue;
  16. class RegistrationFormType extends AbstractType
  17. {
  18. private YamlReader $yamlReader;
  19. private UserField $userField;
  20. public function __construct(
  21. YamlReader $yamlReader,
  22. UserField $userField
  23. )
  24. {
  25. $this->yamlReader = $yamlReader;
  26. $this->userField = $userField;
  27. }
  28. /**
  29. * @param FormBuilderInterface $builder
  30. * @param array $options
  31. *
  32. * @return void
  33. *
  34. * @throws Exception
  35. */
  36. public function buildForm( FormBuilderInterface $builder, array $options ): void
  37. {
  38. $globalFormType = $this->yamlReader->getFormType();
  39. $userType = $globalFormType[ 'user' ][ 'register' ] ?? NULL;
  40. if ( !is_null( $userType ) ) {
  41. foreach ( $userType[ 'fields' ] as $key => $fieldOptions ) {
  42. if ( isset( $fieldOptions[ 'enabled' ] ) && $fieldOptions[ 'enabled' ] === TRUE ) {
  43. $field = $this->userField->getField( $key, $fieldOptions );
  44. $builder->add( $key, $field->getFormType(), $field->toArray() );
  45. }
  46. }
  47. } else {
  48. $builder
  49. ->add( 'email', EmailType::class, [
  50. 'label' => 'E-mail',
  51. 'attr' => [
  52. 'data-parsley-type' => 'email',
  53. ],
  54. 'required' => TRUE,
  55. ] )
  56. ->add( "phone", TextType::class, [
  57. "required" => TRUE,
  58. 'attr' => [
  59. 'placeholder' => '00 00 00 00 00',
  60. 'data-parsley-pattern' => "^0[1-9] \d{2} \d{2} \d{2} \d{2}$",
  61. 'data-inputmask' => "'mask': '99 99 99 99 99'",
  62. 'inputmode' => "text",
  63. ],
  64. ] )
  65. ->add( 'lastName', TextType::class, [
  66. 'label' => 'Nom',
  67. 'required' => TRUE,
  68. ] )
  69. ->add( 'firstName', TextType::class, [
  70. 'label' => 'Prénom',
  71. 'required' => TRUE,
  72. ] )
  73. ->add( 'companySiret', TextType::class, [
  74. 'label' => 'SIRET',
  75. 'required' => TRUE,
  76. ] )
  77. ->add( 'company', TextType::class, [
  78. 'label' => 'Société',
  79. 'required' => TRUE,
  80. ] )
  81. ->add( 'address1', TextType::class, [
  82. 'label' => 'Adresse',
  83. 'required' => TRUE,
  84. ] )
  85. ->add( 'postcode', TextType::class, [
  86. 'label' => 'Code postal',
  87. 'attr' => [
  88. 'data-parsley-length' => '[4,5]',
  89. 'data-parsley-type' => 'number',
  90. 'maxlength' => '5',
  91. ],
  92. 'required' => TRUE,
  93. ] )
  94. ->add( 'city', TextType::class, [
  95. 'label' => 'Ville',
  96. 'required' => TRUE,
  97. ] )
  98. ->add( 'newsletter', CheckboxType::class, [
  99. 'label' => 'Je souhaite recevoir la Newsletter REHAU',
  100. 'required' => FALSE,
  101. ] )
  102. ->add( 'cguAt', CheckboxType::class, [
  103. 'label' => "J'ai lu et j'accepte le <a href='/static-file/general_documents/rehau-pro-reglement-def-30-09-21-61c4b65bc27b9335425151.pdf' targer='blank'>règlement de la plateforme REHAU Pro</a> ",
  104. 'label_html' => TRUE,
  105. 'mapped' => FALSE,
  106. 'constraints' => [
  107. new IsTrue( [
  108. 'message' => 'Vous devez accepter le règlement de la plateforme continuer votre inscription',
  109. ] ),
  110. ],
  111. ] )
  112. ->add( 'companyIsOk', CheckboxType::class, [
  113. 'label' => "J'atteste être le dirigeant de l’entreprise, ou à défaut avoir informé le dirigeant de mon inscription au Club REHAU expert fenêtre",
  114. 'mapped' => FALSE,
  115. 'constraints' => [
  116. new IsTrue( [
  117. 'message' => 'Vous devez accepter pour continuer votre inscription',
  118. ] ),
  119. ],
  120. ] )
  121. ->add( 'password', RepeatedType::class, [
  122. 'type' => PasswordType::class,
  123. 'first_options' => [
  124. 'label' => 'Créer mon mot de passe',
  125. 'label_attr' => [
  126. 'class' => 'custom-account-label',
  127. ],
  128. 'attr' => [
  129. 'autocomplete' => 'new-password',
  130. 'data-parsley-error-message' => 'Le mot de passe doit contenir au moins 8 caractères, 1 majuscule, 1 minuscule, 1 chiffre et un caractère spécial @ $ ! % * ? &',
  131. 'data-parsley-pattern' => "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[^A-Za-z0-9]).{8,}$",
  132. 'class' => 'custom-account-input',
  133. ],
  134. ],
  135. 'second_options' => [
  136. 'label' => 'Confirmer mon mot de passe',
  137. 'label_attr' => [
  138. 'class' => 'custom-account-label',
  139. ],
  140. 'attr' => [
  141. 'class' => 'custom-account-input',
  142. ],
  143. ],
  144. 'invalid_message' => 'Les champs de mot de passe doivent correspondre.',
  145. // instead of being set onto the object directly,
  146. // this is read and encoded in the controller
  147. 'mapped' => FALSE,
  148. 'required' => TRUE,
  149. ] )
  150. ;
  151. }
  152. }
  153. /**
  154. * @param OptionsResolver $resolver
  155. *
  156. * @return void
  157. */
  158. public function configureOptions( OptionsResolver $resolver ): void
  159. {
  160. $resolver->setDefaults(
  161. [
  162. 'data_class' => User::class,
  163. ]
  164. );
  165. }
  166. }