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\GreaterThan; |
12
|
|
|
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual; |
13
|
|
|
use Symfony\Component\Validator\Constraints\Range; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Warnar Boekkooi <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class MinRule implements ConstraintMapperInterface |
19
|
|
|
{ |
20
|
|
|
const RULE_NAME = 'min'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
* @param |
25
|
|
|
*/ |
26
|
5 |
|
public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection) |
27
|
|
|
{ |
28
|
5 |
|
$constraintClass = get_class($constraint); |
29
|
|
|
|
30
|
|
|
/** @var \Symfony\Component\Validator\Constraints\AbstractComparison $constraint */ |
31
|
5 |
|
if ($constraintClass === GreaterThan::class) { |
32
|
1 |
|
$rule = new ConstraintRule( |
33
|
1 |
|
self::RULE_NAME, |
34
|
1 |
|
$constraint->value + 1, // TODO support floats |
35
|
1 |
|
new RuleMessage($constraint->message, array('{{ compared_value }}' => $constraint->value)), |
36
|
1 |
|
$constraint->groups |
37
|
1 |
|
); |
38
|
5 |
|
} elseif ($constraintClass === GreaterThanOrEqual::class) { |
39
|
1 |
|
$rule = new ConstraintRule( |
40
|
1 |
|
self::RULE_NAME, |
41
|
1 |
|
$constraint->value, |
42
|
1 |
|
new RuleMessage($constraint->message, array('{{ compared_value }}' => $constraint->value)), |
43
|
1 |
|
$constraint->groups |
44
|
1 |
|
); |
45
|
1 |
|
} |
46
|
|
|
/** @var Range $constraint */ |
47
|
3 |
|
elseif ($constraintClass === Range::class && $constraint->min !== null) { |
48
|
1 |
|
$rule = new ConstraintRule( |
49
|
1 |
|
self::RULE_NAME, |
50
|
1 |
|
$constraint->min, |
51
|
1 |
|
new RuleMessage($constraint->minMessage, array('{{ limit }}' => $constraint->min)), |
52
|
1 |
|
$constraint->groups |
53
|
1 |
|
); |
54
|
1 |
|
} else { |
55
|
2 |
|
throw new LogicException(); |
56
|
|
|
} |
57
|
|
|
|
58
|
3 |
|
$collection->set( |
59
|
3 |
|
self::RULE_NAME, |
60
|
|
|
$rule |
61
|
3 |
|
); |
62
|
3 |
|
} |
63
|
|
|
|
64
|
5 |
|
public function supports(Constraint $constraint, FormInterface $form) |
65
|
|
|
{ |
66
|
5 |
|
$constraintClass = get_class($constraint); |
67
|
|
|
|
68
|
|
|
return |
69
|
5 |
|
in_array($constraintClass, array( |
70
|
5 |
|
GreaterThan::class, |
71
|
5 |
|
GreaterThanOrEqual::class, |
72
|
5 |
|
), true) || |
73
|
5 |
|
$constraintClass === Range::class && $constraint->min !== null; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|