forked from boekkooi/JqueryValidationBundle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEmailRule.php
45 lines (40 loc) · 1.36 KB
/
EmailRule.php
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
<?php
namespace Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping;
use Boekkooi\Bundle\JqueryValidationBundle\Exception\LogicException;
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\ConstraintRule;
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\ConstraintMapperInterface;
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleCollection;
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleMessage;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Email;
/**
* @author Warnar Boekkooi <[email protected]>
*/
class EmailRule implements ConstraintMapperInterface
{
const RULE_NAME = 'email';
/**
* {@inheritdoc}
*/
public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection)
{
if (!$this->supports($constraint, $form)) {
throw new LogicException();
}
/** @var Email $constraint */
$collection->set(
self::RULE_NAME,
new ConstraintRule(
self::RULE_NAME,
true,
new RuleMessage($constraint->message),
$constraint->groups
)
);
}
public function supports(Constraint $constraint, FormInterface $form)
{
return get_class($constraint) === Email::class;
}
}