forked from boekkooi/JqueryValidationBundle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMinLengthRule.php
67 lines (60 loc) · 2.27 KB
/
MinLengthRule.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
57
58
59
60
61
62
63
64
65
66
67
<?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 Boekkooi\Bundle\JqueryValidationBundle\Form\Util\FormHelper;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\Length;
/**
* @author Warnar Boekkooi <[email protected]>
*/
class MinLengthRule implements ConstraintMapperInterface
{
const RULE_NAME = 'minlength';
/**
* {@inheritdoc}
*/
public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection)
{
if (!$this->supports($constraint, $form)) {
throw new LogicException();
}
/** @var Choice|Length $constraint */
$collection->set(
self::RULE_NAME,
new ConstraintRule(
self::RULE_NAME,
$constraint->min,
new RuleMessage($constraint->minMessage, array('{{ limit }}' => $constraint->min), (int) $constraint->min),
$constraint->groups
)
);
}
public function supports(Constraint $constraint, FormInterface $form)
{
/** @var Choice|Length $constraint */
$constraintClass = get_class($constraint);
if (!in_array($constraintClass, array(Choice::class, Length::class), true) ||
$constraint->min === null ||
$constraint->min == $constraint->max) {
return false;
}
return !(
$constraintClass === Length::class &&
(FormHelper::isSymfony3Compatible() ?
$this->isType($form, ChoiceType::class) :
$this->isType($form, 'choice')
)
);
}
protected function isType(FormInterface $type, $typeName)
{
return FormHelper::isType($type->getConfig()->getType(), $typeName);
}
}