]> Raphaël G. Git Repositories - userbundle/blob - Form/RegisterType.php
09eea5c6bf4eddf403e3853bd416ed6e6c0a41e3
[userbundle] / Form / RegisterType.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys UserBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\UserBundle\Form;
13
14 use Rapsys\PackBundle\Form\CaptchaType;
15
16 use Rapsys\UserBundle\Entity\Civility;
17
18 use Symfony\Bridge\Doctrine\Form\Type\EntityType;
19 use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
20 use Symfony\Component\Form\Extension\Core\Type\EmailType;
21 use Symfony\Component\Form\Extension\Core\Type\PasswordType;
22 use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
23 use Symfony\Component\Form\Extension\Core\Type\SubmitType;
24 use Symfony\Component\Form\Extension\Core\Type\TextType;
25 use Symfony\Component\Form\FormBuilderInterface;
26 use Symfony\Component\OptionsResolver\OptionsResolver;
27 use Symfony\Component\Validator\Constraints\Email;
28 use Symfony\Component\Validator\Constraints\NotBlank;
29
30 /**
31 * {@inheritdoc}
32 */
33 class RegisterType extends CaptchaType {
34 /**
35 * {@inheritdoc}
36 */
37 public function buildForm(FormBuilderInterface $builder, array $options): void {
38 //Add extra mail field
39 if (!empty($options['mail'])) {
40 $builder->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 }
42
43 //Add extra password field
44 if (!empty($options['password'])) {
45 //Add password repeated field
46 if (!empty($options['password_repeated'])) {
47 $builder->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]);
48 //Add password field
49 } else {
50 $builder->add('password', PasswordType::class, ['attr' => ['placeholder' => 'Your password'], 'constraints' => [new NotBlank(['message' => 'Please provide your password'])], 'required' => true]);
51 }
52 }
53
54 //Add extra civility field
55 if (!empty($options['civility'])) {
56 $builder->add('civility', EntityType::class, ['class' => $options['civility_class'], 'attr' => ['placeholder' => 'Your civility'], 'choice_translation_domain' => true, 'empty_data' => $options['civility_default'], 'required' => true]);
57 }
58
59 //Add extra forename field
60 if (!empty($options['forename'])) {
61 $builder->add('forename', TextType::class, ['attr' => ['placeholder' => 'Your forename']]);
62 }
63
64 //Add extra surname field
65 if (!empty($options['surname'])) {
66 $builder->add('surname', TextType::class, ['attr' => ['placeholder' => 'Your surname']]);
67 }
68
69 //Add extra active field
70 if (!empty($options['active'])) {
71 $builder->add('active', CheckboxType::class, ['attr' => ['placeholder' => 'Your active']]);
72 }
73
74 //Add extra enable field
75 if (!empty($options['enable'])) {
76 $builder->add('enable', CheckboxType::class, ['attr' => ['placeholder' => 'Your enable']]);
77 }
78
79 //Add submit
80 $builder->add('submit', SubmitType::class, ['label' => 'Send', 'attr' => ['class' => 'submit']]);
81
82 //Call parent
83 parent::buildForm($builder, $options);
84 }
85
86 /**
87 * {@inheritdoc}
88 */
89 public function configureOptions(OptionsResolver $resolver): void {
90 //Set defaults
91 $resolver->setDefaults(['error_bubbling' => true, 'civility' => true, 'civility_class' => 'RapsysUserBundle:Civility', 'civility_default' => null, 'mail' => true, 'password' => true, 'password_repeated' => true, 'forename' => true, 'surname' => true, 'active' => false, 'enable' => false]);
92
93 //Add extra civility option
94 $resolver->setAllowedTypes('civility', 'boolean');
95
96 //Add civility class
97 $resolver->setAllowedTypes('civility_class', 'string');
98
99 //Add civility default
100 //XXX: trigger strange error about table not existing is not specified in form create
101 $resolver->setAllowedTypes('civility_default', [Civility::class, 'null']);
102
103 //Add extra mail option
104 $resolver->setAllowedTypes('mail', 'boolean');
105
106 //Add extra password option
107 $resolver->setAllowedTypes('password', 'boolean');
108
109 //Add extra password repeated option
110 $resolver->setAllowedTypes('password_repeated', 'boolean');
111
112 //Add extra forename option
113 $resolver->setAllowedTypes('forename', 'boolean');
114
115 //Add extra surname option
116 $resolver->setAllowedTypes('surname', 'boolean');
117
118 //Add extra active option
119 $resolver->setAllowedTypes('active', 'boolean');
120
121 //Add extra enable option
122 $resolver->setAllowedTypes('enable', 'boolean');
123 }
124
125 /**
126 * {@inheritdoc}
127 */
128 public function getName(): string {
129 return 'rapsysuser_register';
130 }
131 }