forked from boekkooi/JqueryValidationBundle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUrlRule.php
52 lines (46 loc) · 1.65 KB
/
UrlRule.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
46
47
48
49
50
51
52
<?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\Url;
/**
* @author Warnar Boekkooi <[email protected]>
*/
class UrlRule implements ConstraintMapperInterface
{
const RULE_NAME = 'url';
/**
* {@inheritdoc}
*/
public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection)
{
/** @var \Symfony\Component\Validator\Constraints\Url $constraint */
if (!$this->supports($constraint, $form)) {
throw new LogicException();
}
// jquery validate only validates https, http, ftp
// So don't add the rule if there is some other protocol
$diff = array_diff($constraint->protocols, array('http', 'https', 'ftp'));
if (!empty($diff)) {
return;
}
$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) === Url::class;
}
}