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