src/Form/UserCreateType.php line 20

  1. <?php
  2. namespace App\Form;
  3. use Symfony\Component\Validator\Constraints\Length;
  4. use App\Entity\User;
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  7. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  8. use Symfony\Component\Form\Extension\Core\Type\TelType;
  9. use Symfony\Component\Form\FormBuilderInterface;
  10. use Symfony\Component\Validator\Constraints\File;
  11. use Symfony\Component\OptionsResolver\OptionsResolver;
  12. use Symfony\Component\Validator\Constraints\NotBlank;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. use Symfony\Component\Form\Extension\Core\Type\FileType;
  15. use Symfony\Component\Form\Extension\Core\Type\TextType;
  16. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  17. class UserCreateType extends AbstractType
  18. {
  19.     public function __construct(private TranslatorInterface $translator)
  20.     {
  21.     }
  22.     public function buildForm(FormBuilderInterface $builder, array $options): void
  23.     {
  24.         $builder
  25.             ->add('first_name'TextType::class, [
  26.                 'label' => $this->translator->trans('Prénom'),
  27.                 'required' => true,
  28.                 'attr' => [
  29.                     'class' => 'form-control mb-4',
  30.                     'placeholder' => ''
  31.                 ],
  32.                 'help' => ''
  33.             ])
  34.             ->add('last_name'TextType::class, [
  35.                 'label' => $this->translator->trans('Nom'),
  36.                 //'Last name',
  37.                 'required' => true,
  38.                 'attr' => [
  39.                     'class' => 'form-control mb-4',
  40.                     'placeholder' => ''
  41.                 ],
  42.                 'help' => ''
  43.             ])
  44.             ->add('phone'TelType::class, [
  45.                 'invalid_message' => $this->translator->trans('Email non valid'),
  46.                 //'Email not valid',
  47.                 'required' => true,
  48.                 'disabled' => false,
  49.                 'label' => $this->translator->trans('Téléphone'),
  50.                 //'Email',
  51.                 'attr' => [
  52.                     'class' => 'form-control mb-4',
  53.                     'placeholder' => ''
  54.                 ],
  55.                 'help' => ''
  56.             ])
  57.             ->add('email'EmailType::class, [
  58.                 'invalid_message' => $this->translator->trans('Email non valid'),
  59.                 //'Email not valid',
  60.                 'required' => true,
  61.                 'disabled' => false,
  62.                 'label' => $this->translator->trans('Email'),
  63.                 //'Email',
  64.                 'attr' => [
  65.                     'class' => 'form-control mb-4',
  66.                     'placeholder' => ''
  67.                 ],
  68.                 'help' => ''
  69.             ])
  70.             ->add('plainPassword'RepeatedType::class, [
  71.                 'type' => PasswordType::class,
  72.                 'first_options' => [
  73.                     'attr' => ['autocomplete' => 'new-password''class' => 'form-control mb-4'],
  74.                     'constraints' => [
  75.                         new NotBlank([
  76.                             'message' => $this->translator->trans('Veuillez entrer un mot de passe'),
  77.                         ]),
  78.                         new Length([
  79.                             'min' => 6,
  80.                             'minMessage' => $this->translator->trans("Votre mot de passe doit comporter au moins {{ limit }} caractères"),
  81.                             // max length allowed by Symfony for security reasons
  82.                             'max' => 4096,
  83.                         ]),
  84.                     ],
  85.                     'label' => $this->translator->trans('Nouveau mot de passe'),
  86.                 ],
  87.                 'second_options' => [
  88.                     'attr' => ['autocomplete' => 'new-password''class' => 'form-control mb-4'],
  89.                     'label' => $this->translator->trans('Répéter le mot de passe'),
  90.                 ],
  91.                 'invalid_message' => $this->translator->trans('Les champs du mot de passe doivent correspondre.'),
  92.                 // Instead of being set onto the object directly,
  93.                 // this is read and encoded in the controller
  94.                 'mapped' => false,
  95.             ])
  96.             // ->add('password')
  97.             ->add('photo'FileType::class, [
  98.                 'label' => $this->translator->trans('Photo d\'identité'),
  99.                 'attr' => [
  100.                     'class' => 'form-control mb-4',
  101.                     'placeholder' => ''
  102.                 ],
  103.                 'help' => '',
  104.                 'mapped' => false,
  105.                 'required' => false,
  106.                 'constraints' => [
  107.                     new File([
  108.                         'maxSize' => '2048k',
  109.                         'mimeTypes' => [
  110.                             'image/*',
  111.                         ],
  112.                         'mimeTypesMessage' => $this->translator->trans('Veuillez télécharger une image valide'),
  113.                     ])
  114.                 ],
  115.             ]);
  116.     }
  117.     public function configureOptions(OptionsResolver $resolver): void
  118.     {
  119.         $resolver->setDefaults([
  120.             'data_class' => User::class,
  121.         ]);
  122.     }
  123. }