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