forked from boekkooi/JqueryValidationBundle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNumberRule.php
56 lines (50 loc) · 1.82 KB
/
NumberRule.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
53
54
55
56
<?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\Range;
use Symfony\Component\Validator\Constraints\Type;
/**
* @author Warnar Boekkooi <[email protected]>
*/
class NumberRule implements ConstraintMapperInterface
{
const RULE_NAME = 'number';
/**
* {@inheritdoc}
*/
public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection)
{
if (!$this->supports($constraint, $form)) {
throw new LogicException();
}
$message = null;
if ($constraint instanceof Range) {
$message = new RuleMessage($constraint->invalidMessage);
} elseif ($constraint instanceof Type) {
$message = new RuleMessage($constraint->message, array('{{ type }}' => $constraint->type));
}
$collection->set(
self::RULE_NAME,
new ConstraintRule(
self::RULE_NAME,
true,
$message,
$constraint->groups
)
);
}
public function supports(Constraint $constraint, FormInterface $form)
{
$class = get_class($constraint);
return $class === Range::class || (
$class === Type::class &&
in_array(strtolower($constraint->type), array('int', 'integer', 'float', 'double'), true)
);
}
}