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 Boekkooi\Bundle\JqueryValidationBundle\Form\Util\FormHelper; |
10
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
11
|
|
|
use Symfony\Component\Form\FormInterface; |
12
|
|
|
use Symfony\Component\Validator\Constraint; |
13
|
|
|
use Symfony\Component\Validator\Constraints\Choice; |
14
|
|
|
use Symfony\Component\Validator\Constraints\Length; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Warnar Boekkooi <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class MinLengthRule implements ConstraintMapperInterface |
20
|
|
|
{ |
21
|
|
|
const RULE_NAME = 'minlength'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
6 |
|
public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection) |
27
|
|
|
{ |
28
|
6 |
|
if (!$this->supports($constraint, $form)) { |
29
|
4 |
|
throw new LogicException(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** @var Choice|Length $constraint */ |
33
|
2 |
|
$collection->set( |
34
|
2 |
|
self::RULE_NAME, |
35
|
2 |
|
new ConstraintRule( |
36
|
2 |
|
self::RULE_NAME, |
37
|
2 |
|
$constraint->min, |
38
|
2 |
|
new RuleMessage($constraint->minMessage, array('{{ limit }}' => $constraint->min), (int) $constraint->min), |
39
|
2 |
|
$constraint->groups |
40
|
2 |
|
) |
41
|
2 |
|
); |
42
|
2 |
|
} |
43
|
|
|
|
44
|
13 |
|
public function supports(Constraint $constraint, FormInterface $form) |
45
|
|
|
{ |
46
|
|
|
/** @var Choice|Length $constraint */ |
47
|
13 |
|
$constraintClass = get_class($constraint); |
48
|
13 |
|
if (!in_array($constraintClass, array(Choice::class, Length::class), true) || |
49
|
13 |
|
$constraint->min === null || |
50
|
13 |
|
$constraint->min == $constraint->max) { |
51
|
8 |
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return !( |
55
|
5 |
|
$constraintClass === Length::class && |
56
|
3 |
|
(FormHelper::isSymfony3Compatible() ? |
57
|
3 |
|
$this->isType($form, ChoiceType::class) : |
58
|
3 |
|
$this->isType($form, 'choice') |
59
|
|
|
) |
60
|
5 |
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
protected function isType(FormInterface $type, $typeName) |
64
|
|
|
{ |
65
|
3 |
|
return FormHelper::isType($type->getConfig()->getType(), $typeName); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|