]> Raphaƫl G. Git Repositories - userbundle/blob - Form/RegisterType.php
Update doc
[userbundle] / Form / RegisterType.php
1 <?php
2
3 namespace Rapsys\UserBundle\Form;
4
5 use Symfony\Bridge\Doctrine\Form\Type\EntityType;
6 use Symfony\Component\Form\AbstractType;
7 use Symfony\Component\Form\Extension\Core\Type\EmailType;
8 use Symfony\Component\Form\Extension\Core\Type\PasswordType;
9 use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
10 use Symfony\Component\Form\Extension\Core\Type\SubmitType;
11 use Symfony\Component\Form\Extension\Core\Type\TextType;
12 use Symfony\Component\Form\FormBuilderInterface;
13 use Symfony\Component\OptionsResolver\OptionsResolver;
14 use Symfony\Component\Validator\Constraints\Email;
15 use Symfony\Component\Validator\Constraints\NotBlank;
16
17 use Rapsys\UserBundle\Entity\Civility;
18
19 class RegisterType extends AbstractType {
20 /**
21 * {@inheritdoc}
22 */
23 public function buildForm(FormBuilderInterface $builder, array $options) {
24 //Create form
25 $form = $builder;
26
27 //Add extra mail field
28 if (!empty($options['mail'])) {
29 $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'])]]);
30 }
31
32 //Add extra civility field
33 if (!empty($options['civility'])) {
34 $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']]);
35 }
36
37 //Add extra pseudonym field
38 if (!empty($options['pseudonym'])) {
39 $form->add('pseudonym', TextType::class, ['attr' => ['placeholder' => 'Your pseudonym'], 'constraints' => [new NotBlank(['message' => 'Please provide your pseudonym'])]]);
40 }
41
42 //Add extra forename field
43 if (!empty($options['forename'])) {
44 $form->add('forename', TextType::class, ['attr' => ['placeholder' => 'Your forename'], 'constraints' => [new NotBlank(['message' => 'Please provide your forename'])]]);
45 }
46
47 //Add extra surname field
48 if (!empty($options['surname'])) {
49 $form->add('surname', TextType::class, ['attr' => ['placeholder' => 'Your surname'], 'constraints' => [new NotBlank(['message' => 'Please provide your surname'])]]);
50 }
51
52 //Add extra password field
53 if (!empty($options['password'])) {
54 $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'])]]]);
55 }
56
57 //Add submit
58 $form->add('submit', SubmitType::class, ['label' => 'Send', 'attr' => ['class' => 'submit']]);
59
60 //Return form
61 return $form;
62 }
63
64 /**
65 * {@inheritdoc}
66 */
67 public function configureOptions(OptionsResolver $resolver) {
68 //Set defaults
69 $resolver->setDefaults(['error_bubbling' => true, 'civility_class' => 'RapsysUserBundle:Civility', 'civility_default' => null, 'mail' => true, 'civility' => true, 'pseudonym' => true, 'forename' => true, 'surname' => true, 'password' => true]);
70
71 //Add civility class
72 $resolver->setAllowedTypes('civility_class', 'string');
73
74 //Add civility default
75 //XXX: trigger strange error about table not existing is not specified in form create
76 $resolver->setAllowedTypes('civility_default', [Civility::class, 'null']);
77
78 //Add extra mail option
79 $resolver->setAllowedTypes('mail', 'boolean');
80
81 //Add extra civility option
82 $resolver->setAllowedTypes('civility', 'boolean');
83
84 //Add extra pseudonym option
85 $resolver->setAllowedTypes('pseudonym', 'boolean');
86
87 //Add extra forename option
88 $resolver->setAllowedTypes('forename', 'boolean');
89
90 //Add extra surname option
91 $resolver->setAllowedTypes('surname', 'boolean');
92
93 //Add extra password option
94 $resolver->setAllowedTypes('password', 'boolean');
95
96 //Return resolver
97 return $resolver;
98 }
99
100 /**
101 * {@inheritdoc}
102 */
103 public function getName() {
104 return 'rapsys_user_register';
105 }
106 }