src/Form/RegistrationFormType.php line 26
<?php
namespace App\Form;
use App\Entity\AppConstants;
use App\Entity\Inscription;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;
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\CountryType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
class RegistrationFormType extends AbstractType
{
public function __construct(private EntityManagerInterface $em)
{
// parent::__construct();
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('username')
->add('firstname', TextType::class, [
'required' => true,
'constraints' => [
new Assert\NotBlank(),
]
])
->add('lastname', TextType::class, [
'required' => true,
'constraints' => [
new Assert\NotBlank(),
]
])
->add('title', ChoiceType::class, [
'required' => true,
'choices' => ['Choose your title' => "",] + array_combine(AppConstants::TITLE_LIST, AppConstants::TITLE_LIST),
'constraints' => [
new Assert\NotBlank(),
]
])
->add('email', EmailType::class, [
'required' => true,
'constraints' => [
new Assert\NotBlank(),
new Assert\Email(),
new Assert\Length([
'min' => 5,
]),
new Assert\Callback(function ($payload, ExecutionContextInterface $context) use ($options) {
/** @var Inscription $data */
$data = $context->getRoot()->getData();
/** @var Inscription $inscription */
/** @var QueryBuilder $qb */
$qb = $this->em->getRepository(Inscription::class)->createQueryBuilder('p')
->join('p.evenement', 'e')
->where('p.mail = :mail')
->setParameter('mail', $payload)
// ->orderBy('p.arg', 'ASC')
->setMaxResults(1);
$qb->andWhere('p.status = :status')
->setParameter('status', Inscription::STATUS_VALID);
if (!empty($options['evenement_id'])) {
$qb->andWhere('e.id = :evenement_id')
->setParameter('evenement_id', $options['evenement_id']);
if (!empty($options['page']) && $options['page'] == 'edit' && $data && $inscription_id = $data->getId()) {
$qb->andWhere('p.id != :id')
->setParameter('id', $data->getId());
}
$inscription = $qb->getQuery()->getOneOrNullResult();
if ($inscription) {
$context->addViolation('Cette adresse e-mail est déjà utilisée par un autre participant', []); // utilisateur
}
}
if (function_exists('dump')) :
// dump(@compact('data', 'payload', 'inscription', 'context', 'inscription_id', 'inscription', 'options'));
endif;
}),
]
])
->add('sexe', ChoiceType::class, [
'required' => true,
'choices' => [
'Choose your sexe' => "",
'Homme' => Inscription::SEXE_HOMME,
'Femme' => Inscription::SEXE_FEMME,
],
'constraints' => [
new Assert\NotBlank(),
new Assert\Choice(choices: AppConstants::SEXE_LIST),
]
])
->add('speciality', ChoiceType::class, [
'required' => true,
'choices' => ['Choose your speciality' => "",] + array_combine(AppConstants::SPECIALITY_LIST, AppConstants::SPECIALITY_LIST),
'constraints' => [
new Assert\NotBlank(),
new Assert\Choice(choices: AppConstants::SPECIALITY_LIST),
]
])
->add('activity', ChoiceType::class, [
'required' => true,
'choices' => ['Choose your activity' => "",] + array_combine(AppConstants::ACTIVITY_LIST, AppConstants::ACTIVITY_LIST),
'constraints' => [
new Assert\NotBlank(),
new Assert\Choice(choices: AppConstants::ACTIVITY_LIST),
]
])
->add('mobile', TextType::class, [
'required' => true,
'constraints' => [
new Assert\NotBlank(),
new Assert\Length([
'min' => 8,
'minMessage' => 'Le numéro de télephone doit contenir au minimum {{ limit }} chiffres',
])
]
])
->add('country', CountryType::class, [
'required' => true,
'preferred_choices' => [
'TN',
'FR',
'DZ',
'MA',
],
'constraints' => [
new Assert\NotBlank(),
]
])
->add('adresse', TextType::class, [
'required' => true,
'constraints' => [
new Assert\NotBlank(),
]
])
->add('city', TextType::class, [
'required' => true,
'constraints' => [
new Assert\NotBlank(),
]
])
->add('postalCode', TextType::class, [
'required' => true,
'constraints' => [
new Assert\NotBlank(),
]
])
->add('workplace', TextType::class, [
'required' => true,
'constraints' => [
new Assert\NotBlank(),
]
])
->add('password', RepeatedType::class,
[
'type' => PasswordType::class,
'mapped'=>false,
'invalid_message'=> 'Please check password',
'label'=> 'Password',
'required'=> true,
'first_options'=> ['label'=> 'Tapez un mot de passe'],
'second_options'=> ['label'=> 'Retapez votre mot de passe' ]
]
)
// ->add('demandeMembership', HiddenType::class, [
// 'data' => 0,
// ])
->add('demandeMembership', ChoiceType::class, [
'label' => false,
'required' => false,
'expanded' => true,
'multiple' => false,
'choices'=>[
'Become a member of the STP'=>1
],
'placeholder'=>false,
])
->add('save', SubmitType::class, ['label' => 'Submit','attr'=>['class'=>'btn btn-primary']]);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}