]> Raphaƫl G. Git Repositories - userbundle/blob - Form/LoginType.php
Add strict type
[userbundle] / Form / LoginType.php
1 <?php
2
3 namespace Rapsys\UserBundle\Form;
4
5 use Symfony\Component\Form\AbstractType;
6 use Symfony\Component\Form\Extension\Core\Type\EmailType;
7 use Symfony\Component\Form\Extension\Core\Type\PasswordType;
8 use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
9 use Symfony\Component\Form\Extension\Core\Type\SubmitType;
10 use Symfony\Component\Form\FormBuilderInterface;
11 use Symfony\Component\OptionsResolver\OptionsResolver;
12 use Symfony\Component\Validator\Constraints\Email;
13 use Symfony\Component\Validator\Constraints\NotBlank;
14
15 class LoginType extends AbstractType {
16 /**
17 * {@inheritdoc}
18 */
19 public function buildForm(FormBuilderInterface $builder, array $options) {
20 //Create form
21 $form = $builder;
22
23 //Add extra mail field
24 if (!empty($options['mail'])) {
25 $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'])]]);
26 }
27
28 //Add extra password field
29 if (!empty($options['password'])) {
30 //Add password repeated field
31 if (!empty($options['password_repeated'])) {
32 $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'])]]]);
33 //Add password field
34 } else {
35 $form->add('password', PasswordType::class, ['attr' => ['placeholder' => 'Your password'], 'constraints' => [new NotBlank(['message' => 'Please provide your password'])]]);
36 }
37 }
38
39 //Add submit
40 $form->add('submit', SubmitType::class, ['label' => 'Send', 'attr' => ['class' => 'submit']]);
41
42 //Return form
43 return $form;
44 }
45
46 /**
47 * {@inheritdoc}
48 */
49 public function configureOptions(OptionsResolver $resolver) {
50 //Set defaults
51 $resolver->setDefaults(['error_bubbling' => true, 'mail' => true, 'password' => true, 'password_repeated' => true]);
52
53 //Add extra mail option
54 $resolver->setAllowedTypes('mail', 'boolean');
55
56 //Add extra password option
57 $resolver->setAllowedTypes('password', 'boolean');
58
59 //Add extra password repeated option
60 $resolver->setAllowedTypes('password_repeated', 'boolean');
61 }
62
63 /**
64 * {@inheritdoc}
65 */
66 public function getName() {
67 return 'rapsys_user_login';
68 }
69 }