]> Raphaël G. Git Repositories - userbundle/blob - Form/RegisterType.php
Add cache and form factory
[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 Symfony\Bridge\Doctrine\Form\Type\EntityType;
15 use Symfony\Component\Form\AbstractType;
16 use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
17 use Symfony\Component\Form\Extension\Core\Type\EmailType;
18 use Symfony\Component\Form\Extension\Core\Type\PasswordType;
19 use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
20 use Symfony\Component\Form\Extension\Core\Type\SubmitType;
21 use Symfony\Component\Form\Extension\Core\Type\TextType;
22 use Symfony\Component\Form\FormBuilderInterface;
23 use Symfony\Component\OptionsResolver\OptionsResolver;
24 use Symfony\Component\Validator\Constraints\Email;
25 use Symfony\Component\Validator\Constraints\NotBlank;
26
27 use Rapsys\UserBundle\Entity\Civility;
28
29 class RegisterType extends AbstractType {
30 /**
31 * {@inheritdoc}
32 */
33 public function buildForm(FormBuilderInterface $builder, array $options): FormBuilderInterface {
34 //Create form
35 $form = $builder;
36
37 //Add extra mail field
38 if (!empty($options['mail'])) {
39 $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]);
40 }
41
42 //Add extra password field
43 if (!empty($options['password'])) {
44 //Add password repeated field
45 if (!empty($options['password_repeated'])) {
46 $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]);
47 //Add password field
48 } else {
49 $form->add('password', PasswordType::class, ['attr' => ['placeholder' => 'Your password'], 'constraints' => [new NotBlank(['message' => 'Please provide your password'])], 'required' => true]);
50 }
51 }
52
53 //Add extra civility field
54 if (!empty($options['civility'])) {
55 $form->add('civility', EntityType::class, ['class' => $options['civility_class'], 'attr' => ['placeholder' => 'Your civility'], 'choice_translation_domain' => true, 'empty_data' => $options['civility_default'], 'required' => true]);
56 }
57
58 //Add extra forename field
59 if (!empty($options['forename'])) {
60 $form->add('forename', TextType::class, ['attr' => ['placeholder' => 'Your forename']]);
61 }
62
63 //Add extra surname field
64 if (!empty($options['surname'])) {
65 $form->add('surname', TextType::class, ['attr' => ['placeholder' => 'Your surname']]);
66 }
67
68 //Add extra active field
69 if (!empty($options['active'])) {
70 $form->add('active', CheckboxType::class, ['attr' => ['placeholder' => 'Your active']]);
71 }
72
73 //Add extra enable field
74 if (!empty($options['enable'])) {
75 $form->add('enable', CheckboxType::class, ['attr' => ['placeholder' => 'Your enable']]);
76 }
77
78 //Add submit
79 $form->add('submit', SubmitType::class, ['label' => 'Send', 'attr' => ['class' => 'submit']]);
80
81 //Return form
82 return $form;
83 }
84
85 /**
86 * {@inheritdoc}
87 */
88 public function configureOptions(OptionsResolver $resolver): void {
89 //Set defaults
90 $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]);
91
92 //Add extra civility option
93 $resolver->setAllowedTypes('civility', 'boolean');
94
95 //Add civility class
96 $resolver->setAllowedTypes('civility_class', 'string');
97
98 //Add civility default
99 //XXX: trigger strange error about table not existing is not specified in form create
100 $resolver->setAllowedTypes('civility_default', [Civility::class, 'null']);
101
102 //Add extra mail option
103 $resolver->setAllowedTypes('mail', 'boolean');
104
105 //Add extra password option
106 $resolver->setAllowedTypes('password', 'boolean');
107
108 //Add extra password repeated option
109 $resolver->setAllowedTypes('password_repeated', 'boolean');
110
111 //Add extra forename option
112 $resolver->setAllowedTypes('forename', 'boolean');
113
114 //Add extra surname option
115 $resolver->setAllowedTypes('surname', 'boolean');
116
117 //Add extra active option
118 $resolver->setAllowedTypes('active', 'boolean');
119
120 //Add extra enable option
121 $resolver->setAllowedTypes('enable', 'boolean');
122 }
123
124 /**
125 * {@inheritdoc}
126 */
127 public function getName(): string {
128 return 'rapsys_user_register';
129 }
130 }