aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Form/Type/ForgotPasswordType.php
blob: 2ee24b8f569cf18086cd28dda4ac3484f61f7e05 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php

namespace Wallabag\CoreBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\ExecutionContextInterface;
use Doctrine\Bundle\DoctrineBundle\Registry;

class ForgotPasswordType extends AbstractType
{
    private $doctrine = null;

    public function __construct(Registry $doctrine)
    {
        $this->doctrine = $doctrine;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('email', 'email', array(
                'constraints' => array(
                    new Constraints\Email(),
                    new Constraints\NotBlank(),
                    new Constraints\Callback(array(array($this, 'validateEmail'))),
                ),
            ))
        ;
    }

    public function getName()
    {
        return 'forgot_password';
    }

    public function validateEmail($email, ExecutionContextInterface $context)
    {
        $user = $this->doctrine
            ->getRepository('WallabagCoreBundle:User')
            ->findOneByEmail($email);

        if (!$user) {
            $context->addViolationAt(
                'email',
                'No user found with this email',
                array(),
                $email
            );
        }
    }
}