<?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\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
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;
use Symfony\Component\Form\CallbackTransformer;
class RegistrationFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('email', EmailType::class, [
'label' => 'Adresse email',
'row_attr' => ['class' => 'form-floating mb-2'],
'attr' => ['class' => 'form-control'],
'label_attr' => ['class' => 'form-label'],
])
->add('firstName', TextType::class, [
'label' => 'Prénom',
'row_attr' => ['class' => 'form-floating mb-2'],
'attr' => ['class' => 'form-control'],
'label_attr' => ['class' => 'form-label'],
])
->add('lastName', TextType::class, [
'label' => 'Nom',
'row_attr' => ['class' => 'form-floating mb-2'],
'attr' => ['class' => 'form-control'],
'label_attr' => ['class' => 'form-label'],
])
->add('plainPassword', RepeatedType::class, [
'type' => PasswordType::class,
'first_options' => [
'label' => 'Mot de passe',
'row_attr' => ['class' => 'form-floating mb-2'],
'attr' => ['class' => 'form-control'],
'label_attr' => ['class' => 'form-label'],
],
'second_options' => [
'label' => 'Repeter le mot de passe',
'row_attr' => ['class' => 'form-floating mb-2'],
'attr' => ['class' => 'form-control'],
'label_attr' => ['class' => 'form-label'],
],
'invalid_message' => 'The password fields must match.',
'required' => true,
'mapped' => false,
'constraints' => [
new NotBlank([
'message' => 'Please enter a password',
]),
new Length([
'min' => 6,
'minMessage' => 'Your password should be at least {{ limit }} characters',
'max' => 4096,
// max length allowed by Symfony for security reasons
]),
],
])
->add('roles', ChoiceType::class, [
'label' => 'Vous etes ...',
'choices' => [
'parent' => 'ROLE_PARENT_REGISTERED',
'ecole' => 'ROLE_SCHOOL_REGISTERED',
],
'required' => true,
'multiple' => false,
'expanded' => false,
'row_attr' => ['class' => 'form-floating mb-2'],
'attr' => ['class' => 'form-control'],
'label_attr' => ['class' => 'form-label'],
])
->add('agreeTerms', CheckboxType::class, [
'label' => 'Accepter les conditions',
'row_attr' => ['class' => 'd-flex mb-2'],
'attr' => ['class' => 'me-2'],
'label_attr' => ['class' => 'form-check-label'],
'mapped' => false,
'constraints' => [
new IsTrue([
'message' => 'You should agree to our terms.',
]),
],
]);
// Data transformer
$builder->get('roles')
->addModelTransformer(new CallbackTransformer(
function ($rolesArray) {
// transform the array to a string
return count($rolesArray)? $rolesArray[0]: null;
},
function ($rolesString) {
// transform the string back to an array
return [$rolesString];
}
));
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}