<?php declare(strict_types=1);
namespace App\Form;
use App\Entity\User;
use App\Utils\CommonConstants;
use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType;
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrue as RecaptchaTrue;
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\TelType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\IsTrue;
/**
* Class RegistrationFormType
* @package App\Form
*/
class RegistrationFormType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$roles_array = [
CommonConstants::ROLE_MANDATARY => CommonConstants::ROLE_ROLE_MANDATARY,
CommonConstants::ROLE_DIRECTOR . ' / ' . CommonConstants::ROLE_ADVISOR => CommonConstants::ROLE_ROLE_DIRECTOR,
CommonConstants::ROLE_COACH => CommonConstants::ROLE_ROLE_COACH
];
$builder
->add('email', EmailType::class, [
"label" => "Email ",
"label_attr" => ["class" => "label-weact col-12", 'style'=>"text-align:start"],
"attr" => ["class" => "form-control form-control-sm border-weact",
"id" => "inputEmail",
"placeholder" => "Email de connexion"
]
])
->add('agreeTerms', CheckboxType::class, [
'mapped' => false,
"label" => "En cochant la case vous acceptez les CGU",
"label_attr" => ["class" => "label-weact ", 'style'=>"text-align:start"],
'constraints' => [
new IsTrue([
'message' => 'Vous devez accepter nos conditions',
])
]
])
->add('password', RepeatedType::class, [
'type' => PasswordType::class,
'invalid_message' => 'Vos Mots de passe doivent correspondre',
'required' => true,
'first_options' => ["label_attr" => ["class" => "label-weact col-12 control-label", 'style'=>"text-align:start"],
"label" => "Mot de passe ",
"attr" => ["class" => "form-control form-control-sm border-weact",
"placeholder" => "Saisissez votre mot de passe"]
],
'second_options' => [
"label" => "Confirmez le mot de passe",
"attr" => [
"class" => "form-control form-control-sm border-weact",
"placeholder" => "Confirmez votre mot de passe"],
'mapped' => true
],
])
->add('civility', ChoiceType::class, [
"placeholder" => "Civilité",
'choices' => [
"M." => "M.",
"Mme" => "Mme"
],
"attr" => [
"class" => "form-control form-control-sm border-weact"
],
'label' => 'Civilité ',
"label_attr" => ["class" => "label-weact col-12", 'style'=>"text-align:start"],
])
->add('lastName', TextType::class, [
'label' => 'Votre nom ',
"label_attr" => ["class" => "label-weact col-12", 'style'=>"text-align:start"],
"attr" => ["class" => "form-control form-control-sm border-weact", 'placeholder' => 'Votre nom']
])
->add('firstName', TextType::class, [
'label' => 'Votre prénom ',
"label_attr" => ["class" => "label-weact col-12", 'style'=>"text-align:start"],
"attr" => ["class" => "form-control form-control-sm border-weact", 'placeholder' => 'Votre prénom']
])
->add('nomEtablissement', TextType::class, [
'label' => 'Nom de votre établissement ',
"label_attr" => ["class" => "label-weact col-12", 'style'=>"text-align:start"],
"mapped"=>false,
'required'=>false,
"attr" => ["class" => "form-control form-control-sm border-weact", 'placeholder' => 'Nom de votre établissement']
])
->add('phoneMobilePro', TelType::class, [
'label' => 'Téléphone portable ',
"label_attr" => ["class" => "label-weact col-12", 'style'=>"text-align:start"],
"attr" => ["class" => "form-control form-control-sm border-weact", 'placeholder' => 'Téléphone mobile']
])
->add('address', TextType::class, [
'label' => 'Adresse ',
"label_attr" => ["class" => "label-weact col-12", 'style'=>"text-align:start"],
"attr" => ["class" => "form-control form-control-sm border-weact", 'placeholder' => 'Votre adresse']
])
->add('addressComplement', TextType::class, [
"required" => false,
"label" => "Complement d'adresse",
"label_attr" => ["class" => "label-weact col-12", 'style'=>"text-align:start"],
"attr" => ["class" => "form-control form-control-sm border-weact", 'placeholder' => 'Complément d\'adresse']
])
->add('zipCode', TextType::class, [
"label" => "Code postal ",
"label_attr" => ["class" => "label-weact col-12", 'style'=>"text-align:start"],
"attr" => ["class" => "form-control form-control-sm border-weact", 'placeholder' => 'CP']
])
->add('city', TextType::class, [
"label" => "Ville ",
"label_attr" => ["class" => "label-weact col-12", 'style'=>"text-align:start"],
"attr" => ["class" => "form-control form-control-sm border-weact", 'placeholder' => 'Ville']
])
->add('roles', ChoiceType::class, [
'mapped' => false,
"placeholder" => "Type de conseiller",
"label_attr" => ["class" => "label-weact col-12", 'style'=>"text-align:start"],
'choices' => $roles_array,
"attr" => [
"class" => "form-control form-control-sm "
]
]);
$builder->add('recaptcha', EWZRecaptchaType::class, [
'mapped' => false,
'constraints' => [new RecaptchaTrue()],
'attr' => [
'options' => [
'theme' => 'light',
'type' => 'image',
'size' => 'normal',
'defer' => true,
'async' => true
]
]]);
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
/** @var User $user */
$user = $event->getForm()->getData();
$data = $event->getData();
$roles = [$data['roles']];
if ($data['roles'] === CommonConstants::ROLE_ROLE_DIRECTOR) {
$roles[] = CommonConstants::ROLE_ROLE_ADVISOR;
}
$user->addRoles($roles);
});
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class,
'allow_extra_fields' => true
]);
}
}