<?php
namespace App\Form;
use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TelType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use App\Validator\Constraints\UniqueEmail;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('civility', ChoiceType::class, [
'label' => 'Type de profil',
'choices' => [
'M.' => 'Monsieur',
'Mme' => 'Madame',
'Entreprise' => 'Entreprise',
'Association' => 'Association'
],
'attr' => [
'class' => 'chosen-select no-search-select pl-125',
],
'placeholder' => '-Choisir votre type de profil-',
'constraints' => [
new NotBlank([
'message' => 'Veuillez entrer votre type de profil !',
]),
],
'required' => true
])
->add('lastname', TextType::class, [
'label' => 'Nom ',
'attr' => [
'maxlength' => 60
],
'constraints' => [
new Length([
'min' => 3,
'minMessage' => 'Le nom doit avoir au moins {{ limit }} caractères.',
]),
new NotBlank([
'message' => 'Veuillez entrer votre nom !',
]),
],
'required' => true
])
->add('firstname', TextType::class, [
'label' => 'Prénom',
'attr' => [
'maxlength' => 60
],
'constraints' => [
new Length([
'min' => 3,
'minMessage' => 'Le prénom doit avoir au moins {{ limit }} caractères.',
]),
new NotBlank([
'message' => 'Veuillez entrer prénom !',
]),
],
'required' => true
])
->add('email', EmailType::class, [
'label' => 'Email',
'attr' => [
'maxlength' => 180
],
'constraints' => [
new NotBlank([
'message' => 'Veuillez entrer votre email !',
]),
new Email([
'message' => 'L\'adresse email "{{ value }}" n\'est pas valide.',
]),
new UniqueEmail([
'message' => 'Cet e-mail est déjà utilisé par un autre compte.',
]),
],
'required' => true,
])
->add('password', RepeatedType::class, [
'mapped' => false,
'attr' => [
'minlength' => 8
],
'constraints' => [
new Length([
'min' => 8,
'minMessage' => 'Le mot de passe doit avoir au moins {{ limit }} caractères.',
]),
new NotBlank([
'message' => 'Veuillez entrer votre mot de passe !',
]),
],
'type' => PasswordType::class,
'invalid_message' => 'Le deux mots de passe ne sont pas identiques !',
'options' => [
'attr' => [
'maxlength' => 255,
'autocomplete' => 'off',
]
],
'required' => true,
'first_options' => [
'label' => 'Mot de passe',
'attr' => [
'placeholder' => 'Mot de passe',
]
],
'second_options' => [
'label' => 'Confirmer le mot de passe',
'attr' => [
'placeholder' => 'Confirmer le mot de passe'
]
]
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}