]> Raphaël G. Git Repositories - userbundle/blob - Form/RegisterType.php
Remove slug support
[userbundle] / Form / RegisterType.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys PackBundle 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\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;
25
26 use Rapsys\UserBundle\Entity\Civility;
27
28 class RegisterType extends AbstractType {
29 /**
30 * {@inheritdoc}
31 */
32 public function buildForm(FormBuilderInterface $builder, array $options): FormBuilderInterface {
33 //Create form
34 $form = $builder;
35
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'])]]);
39 }
40
41 //Add extra civility field
42 if (!empty($options['civility'])) {
43 $form->add('civility', EntityType::class, ['class' => $options['civility_class'], 'attr' => ['placeholder' => 'Your civility'], 'constraints' => [new NotBlank(['message' => 'Please provide your civility'])], 'choice_translation_domain' => true, 'empty_data' => $options['civility_default']]);
44 }
45
46 //Add extra pseudonym field
47 if (!empty($options['pseudonym'])) {
48 $form->add('pseudonym', TextType::class, ['attr' => ['placeholder' => 'Your pseudonym'], 'constraints' => [new NotBlank(['message' => 'Please provide your pseudonym'])]]);
49 }
50
51 //Add extra forename field
52 if (!empty($options['forename'])) {
53 $form->add('forename', TextType::class, ['attr' => ['placeholder' => 'Your forename'], 'constraints' => [new NotBlank(['message' => 'Please provide your forename'])]]);
54 }
55
56 //Add extra surname field
57 if (!empty($options['surname'])) {
58 $form->add('surname', TextType::class, ['attr' => ['placeholder' => 'Your surname'], 'constraints' => [new NotBlank(['message' => 'Please provide your surname'])]]);
59 }
60
61 //Add extra password field
62 if (!empty($options['password'])) {
63 $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'])]]]);
64 }
65
66 //Add extra slug field
67 if (!empty($options['slug'])) {
68 $form->add('slug', TextType::class, ['attr' => ['placeholder' => 'Your slug'], 'required' => false]);
69 }
70
71 //Add submit
72 $form->add('submit', SubmitType::class, ['label' => 'Send', 'attr' => ['class' => 'submit']]);
73
74 //Return form
75 return $form;
76 }
77
78 /**
79 * {@inheritdoc}
80 */
81 public function configureOptions(OptionsResolver $resolver): void {
82 //Set defaults
83 $resolver->setDefaults(['error_bubbling' => true, 'civility_class' => 'RapsysUserBundle:Civility', 'civility_default' => null, 'mail' => true, 'civility' => true, 'pseudonym' => true, 'forename' => true, 'surname' => true, 'password' => true, 'slug' => true]);
84
85 //Add civility class
86 $resolver->setAllowedTypes('civility_class', 'string');
87
88 //Add civility default
89 //XXX: trigger strange error about table not existing is not specified in form create
90 $resolver->setAllowedTypes('civility_default', [Civility::class, 'null']);
91
92 //Add extra mail option
93 $resolver->setAllowedTypes('mail', 'boolean');
94
95 //Add extra civility option
96 $resolver->setAllowedTypes('civility', 'boolean');
97
98 //Add extra pseudonym option
99 $resolver->setAllowedTypes('pseudonym', 'boolean');
100
101 //Add extra forename option
102 $resolver->setAllowedTypes('forename', 'boolean');
103
104 //Add extra surname option
105 $resolver->setAllowedTypes('surname', 'boolean');
106
107 //Add extra password option
108 $resolver->setAllowedTypes('password', 'boolean');
109
110 //Add extra slug option
111 $resolver->setAllowedTypes('slug', 'boolean');
112 }
113
114 /**
115 * {@inheritdoc}
116 */
117 public function getName(): string {
118 return 'rapsys_user_register';
119 }
120 }