1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys UserBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\UserBundle\Form
;
14 use Symfony\Bridge\Doctrine\Form\Type\EntityType
;
15 use Symfony\Component\Form\AbstractType
;
16 use Symfony\Component\Form\Extension\Core\Type\EmailType
;
17 use Symfony\Component\Form\Extension\Core\Type\PasswordType
;
18 use Symfony\Component\Form\Extension\Core\Type\RepeatedType
;
19 use Symfony\Component\Form\Extension\Core\Type\SubmitType
;
20 use Symfony\Component\Form\Extension\Core\Type\TextType
;
21 use Symfony\Component\Form\FormBuilderInterface
;
22 use Symfony\Component\OptionsResolver\OptionsResolver
;
23 use Symfony\Component\Validator\Constraints\Email
;
24 use Symfony\Component\Validator\Constraints\NotBlank
;
26 use Rapsys\UserBundle\Entity\Civility
;
28 class RegisterType
extends AbstractType
{
32 public function buildForm(FormBuilderInterface
$builder, array $options): FormBuilderInterface
{
36 //Add extra mail field
37 if (!empty($options['mail'])) {
38 $form->add('mail', EmailType
::class, ['attr' => ['placeholder' => 'Your mail'], 'constraints' => [new NotBlank(['message' => 'Please provide your mail']), new Email(['message' => 'Your mail doesn\'t seems to be valid'])], 'required' => true]);
41 //Add extra password field
42 if (!empty($options['password'])) {
43 $form->add('password', RepeatedType
::class, ['type' => PasswordType
::class, 'invalid_message' => 'The password and confirmation must match', 'first_options' => ['attr' => ['placeholder' => 'Your password'], 'label' => 'Password'], 'second_options' => ['attr' => ['placeholder' => 'Your password confirmation'], 'label' => 'Confirm password'], 'options' => ['constraints' => [new NotBlank(['message' => 'Please provide your password'])]], 'required' => true]);
46 //Add extra civility field
47 if (!empty($options['civility'])) {
48 $form->add('civility', EntityType
::class, ['class' => $options['civility_class'], 'attr' => ['placeholder' => 'Your civility'], 'choice_translation_domain' => true, 'empty_data' => $options['civility_default'], 'required' => true]);
51 //Add extra forename field
52 if (!empty($options['forename'])) {
53 $form->add('forename', TextType
::class, ['attr' => ['placeholder' => 'Your forename']]);
56 //Add extra surname field
57 if (!empty($options['surname'])) {
58 $form->add('surname', TextType
::class, ['attr' => ['placeholder' => 'Your surname']]);
62 $form->add('submit', SubmitType
::class, ['label' => 'Send', 'attr' => ['class' => 'submit']]);
71 public function configureOptions(OptionsResolver
$resolver): void {
73 $resolver->setDefaults(['error_bubbling' => true, 'civility_class' => 'RapsysUserBundle:Civility', 'civility_default' => null, 'mail' => true, 'civility' => true, 'forename' => true, 'surname' => true, 'password' => true]);
76 $resolver->setAllowedTypes('civility_class', 'string');
78 //Add civility default
79 //XXX: trigger strange error about table not existing is not specified in form create
80 $resolver->setAllowedTypes('civility_default', [Civility
::class, 'null']);
82 //Add extra mail option
83 $resolver->setAllowedTypes('mail', 'boolean');
85 //Add extra civility option
86 $resolver->setAllowedTypes('civility', 'boolean');
88 //Add extra forename option
89 $resolver->setAllowedTypes('forename', 'boolean');
91 //Add extra surname option
92 $resolver->setAllowedTypes('surname', 'boolean');
94 //Add extra password option
95 $resolver->setAllowedTypes('password', 'boolean');
101 public function getName(): string {
102 return 'rapsys_user_register';