<?php
namespace App\Form;
use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
class RegistrationFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('role', ChoiceType::class, [
'required' => true,
'multiple' => false,
'expanded' => false,
'placeholder' => 'Sélectionner *',
'choices' => [
'Je suis chasseur' => 'ROLE_CHASSEUR',
'Je suis hôte' => 'ROLE_HOTE',
'Je suis les deux' => 'ROLE_HOTE_CHASSEUR'
],
])
->add('firstName', null, [
'attr' => [
'class' => 'text-input',
'placeholder' => "Prénom *"
]
])
->add('lastName', null, [
'attr' => [
'class' => 'text-input',
'placeholder' => "Nom *"
]
])
->add('birthday', DateType::class, [
'widget' => 'single_text',
'required' => false,
// prevents rendering it as type="date", to avoid HTML5 date pickers
'html5' => false,
// adds a class that can be selected in JavaScript
'attr' => [
'placeholder' => 'Date de naissance (dd/mm/yyyy)'
],
'format' => 'dd/MM/yyyy',
'years' => range(date('Y')-100, date('Y'))
])
->add('email', EmailType::class, [
'attr' => [
'class' => 'email-input',
'placeholder' => "Email *"
],
'required' => true
])
->add('phone', TelType::class, [
'attr' => [
'class' => 'text-input',
'placeholder' => "Téléphone *"
]
])
->add('plainPassword', RepeatedType::class, [
// instead of being set onto the object directly,
// this is read and encoded in the controller
'mapped' => false,
'type' => PasswordType::class,
'options' => ['attr' => ['autocomplete' => 'new-password']],
'constraints' => [
new NotBlank([
'message' => 'Entrer votre mot de passe svp',
]),
new Length([
'min' => 6,
'minMessage' => 'Votre mot de passe doit avoir au moins {{ limit }} caractères.',
// max length allowed by Symfony for security reasons
'max' => 4096,
]),
],
])
->add('isAcceptNewsletter', CheckboxType::class, [
'label' => false,
'required' => false
])
->add('agreeCgv', CheckboxType::class, [
'mapped' => false,
'label' => false,
'constraints' => [
new IsTrue([
'message' => 'Vous devez accepter les conditions générales de vente.',
]),
],
])
->add('agreeTerms', CheckboxType::class, [
'mapped' => false,
'label' => false,
'constraints' => [
new IsTrue([
'message' => 'Vous devez accepter les politiques de confidentialités.',
]),
],
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}