]> Raphaƫl G. Git Repositories - userbundle/blob - Form/RegisterType.php
Add option to enable each field individualy
[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 $form = $builder;
25
26 //Add extra mail field
27 if (!empty($options['mail'])) {
28 $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'])]]);
29 }
30
31 //Add extra civility field
32 if (!empty($options['civility'])) {
33 $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, 'data' => $options['civility_default']]);
34 }
35
36 //Add extra pseudonym field
37 if (!empty($options['pseudonym'])) {
38 $form->add('pseudonym', TextType::class, ['attr' => ['placeholder' => 'Your pseudonym'], 'constraints' => [new NotBlank(['message' => 'Please provide your pseudonym'])]]);
39 }
40
41 //Add extra forename field
42 if (!empty($options['forename'])) {
43 $form->add('forename', TextType::class, ['attr' => ['placeholder' => 'Your forename'], 'constraints' => [new NotBlank(['message' => 'Please provide your forename'])]]);
44 }
45
46 //Add extra surname field
47 if (!empty($options['surname'])) {
48 $form->add('surname', TextType::class, ['attr' => ['placeholder' => 'Your surname'], 'constraints' => [new NotBlank(['message' => 'Please provide your surname'])]]);
49 }
50
51 //Add extra password field
52 if (!empty($options['password'])) {
53 $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'])]]]);
54 }
55
56 //Add submit
57 $form->add('submit', SubmitType::class, ['label' => 'Send', 'attr' => ['class' => 'submit']]);
58
59 //Return form
60 return $form;
61 }
62
63 /**
64 * {@inheritdoc}
65 */
66 public function configureOptions(OptionsResolver $resolver) {
67 //Set defaults
68 $resolver->setDefaults(['error_bubbling' => true, 'civility_class' => 'RapsysUserBundle:Civility', 'civility_default' => null, 'mail' => true, 'civility' => true, 'pseudonym' => true, 'forename' => true, 'surname' => true, 'password' => true]);
69
70 //Add civility class
71 $resolver->setAllowedTypes('civility_class', 'string');
72
73 //Add civility default
74 //XXX: trigger strange error about table not existing is not specified in form create
75 $resolver->setAllowedTypes('civility_default', [Civility::class, 'null']);
76
77 //Add extra mail option
78 $resolver->setAllowedTypes('mail', 'boolean');
79
80 //Add extra civility option
81 $resolver->setAllowedTypes('civility', 'boolean');
82
83 //Add extra pseudonym option
84 $resolver->setAllowedTypes('pseudonym', 'boolean');
85
86 //Add extra forename option
87 $resolver->setAllowedTypes('forename', 'boolean');
88
89 //Add extra surname option
90 $resolver->setAllowedTypes('surname', 'boolean');
91
92 //Add extra password option
93 $resolver->setAllowedTypes('password', 'boolean');
94
95 //Return resolver
96 return $resolver;
97 }
98
99 /**
100 * {@inheritdoc}
101 */
102 public function getName() {
103 return 'rapsys_user_register';
104 }
105 }