|
1
|
|
|
<?php |
|
2
|
|
|
namespace Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping; |
|
3
|
|
|
|
|
4
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Exception\LogicException; |
|
5
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\ConstraintRule; |
|
6
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\ConstraintMapperInterface; |
|
7
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleCollection; |
|
8
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleMessage; |
|
9
|
|
|
use Symfony\Component\Form\FormInterface; |
|
10
|
|
|
use Symfony\Component\Validator\Constraint; |
|
11
|
|
|
use Symfony\Component\Validator\Constraints\Range; |
|
12
|
|
|
use Symfony\Component\Validator\Constraints\Type; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @author Warnar Boekkooi <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class NumberRule implements ConstraintMapperInterface |
|
18
|
|
|
{ |
|
19
|
|
|
const RULE_NAME = 'number'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* {@inheritdoc} |
|
23
|
|
|
*/ |
|
24
|
4 |
|
public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection) |
|
25
|
|
|
{ |
|
26
|
4 |
|
if (!$this->supports($constraint, $form)) { |
|
27
|
2 |
|
throw new LogicException(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
2 |
|
$message = null; |
|
31
|
2 |
|
if ($constraint instanceof Range) { |
|
32
|
1 |
|
$message = new RuleMessage($constraint->invalidMessage); |
|
33
|
2 |
|
} elseif ($constraint instanceof Type) { |
|
34
|
1 |
|
$message = new RuleMessage($constraint->message, array('{{ type }}' => $constraint->type)); |
|
35
|
1 |
|
} |
|
36
|
2 |
|
$collection->set( |
|
37
|
2 |
|
self::RULE_NAME, |
|
38
|
2 |
|
new ConstraintRule( |
|
39
|
2 |
|
self::RULE_NAME, |
|
40
|
2 |
|
true, |
|
41
|
2 |
|
$message, |
|
42
|
2 |
|
$constraint->groups |
|
43
|
2 |
|
) |
|
44
|
2 |
|
); |
|
45
|
2 |
|
} |
|
46
|
|
|
|
|
47
|
11 |
|
public function supports(Constraint $constraint, FormInterface $form) |
|
48
|
|
|
{ |
|
49
|
11 |
|
$class = get_class($constraint); |
|
50
|
|
|
|
|
51
|
11 |
|
return $class === Range::class || ( |
|
52
|
9 |
|
$class === Type::class && |
|
53
|
5 |
|
in_array(strtolower($constraint->type), array('int', 'integer', 'float', 'double'), true) |
|
54
|
11 |
|
); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|