8000 [Validator] Constraint validators now use the 2.5 API. For incompatib… · symfony/symfony@8e461af · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e461af

Browse files
committed
[Validator] Constraint validators now use the 2.5 API. For incompatible validators, legacy validators were created
1 parent 7e175ef commit 8e461af

11 files changed

+493
-36
lines changed

src/Symfony/Component/Validator/Constraints/AllValidator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ public function validate($value, Constraint $constraint)
4141

4242
$context = $this->context;
4343
$group = $context->getGroup();
44+
$validator = $context->getValidator()->inContext($context);
4445

4546
foreach ($value as $key => $element) {
4647
foreach ($constraint->constraints as $constr) {
47-
$context->validateValue($element, $constr, '['.$key.']', $group);
48+
$validator->atPath('['.$key.']')->validate($element, $constr, $group);
4849
}
4950
}
5051
}

src/Symfony/Component/Validator/Constraints/ChoiceValidator.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,35 @@ public function validate($value, Constraint $constraint)
6363
if ($constraint->multiple) {
6464
foreach ($value as $_value) {
6565
if (!in_array($_value, $choices, $constraint->strict)) {
66-
$this->context->addViolation($constraint->multipleMessage, array('{{ value }}' => $_value));
66+
$this->context->buildViolation($constraint->multipleMessage)
67+
->setParameter('{{ value }}', $_value)
68+
->addViolation();
6769
}
6870
}
6971

7072
$count = count($value);
7173

7274
if ($constraint->min !== null && $count < $constraint->min) {
73-
$this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min), null, (int) $constraint->min);
75+
$this->context->buildViolation($constraint->minMessage)
76+
->setParameter('{{ limit }}', $constraint->min)
77+
->setPlural((int) $constraint->min)
78+
->addViolation();
7479

7580
return;
7681
}
7782

7883
if ($constraint->max !== null && $count > $constraint->max) {
79-
$this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max), null, (int) $constraint->max);
84+
$this->context->buildViolation($constraint->maxMessage)
85+
->setParameter('{{ limit }}', $constraint->max)
86+
->setPlural((int) $constraint->max)
87+
->addViolation();
8088

8189
return;
8290
}
8391
} elseif (!in_array($value, $choices, $constraint->strict)) {
84-
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
92+
$this->context->buildViolation($constraint->message)
93+
->setParameter('{{ value }}', $value)
94+
->addViolation();
8595
}
8696
}
8797
}

src/Symfony/Component/Validator/Constraints/CollectionValidator.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function validate($value, Constraint $constraint)
5050
// to validate() instead.
5151
$context = $this->context;
5252
$group = $context->getGroup();
53+
$validator = $context->getValidator()->inContext($context);
5354

5455
foreach ($constraint->fields as $field => $fieldConstraint) {
5556
if (
@@ -58,21 +59,23 @@ public function validate($value, Constraint $constraint)
5859
($value instanceof \ArrayAccess && $value->offsetExists($field))
5960
) {
6061
foreach ($fieldConstraint->constraints as $constr) {
61-
$context->validateValue($value[$field], $constr, '['.$field.']', $group);
62+
$validator->atPath('['.$field.']')->validate($value[$field], $constr, $group);
6263
}
6364
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
64-
$context->addViolationAt('['.$field.']', $constraint->missingFieldsMessage, array(
65-
'{{ field }}' => $field
66-
), null);
65+
$context->buildViolation($constraint->missingFieldsMessage)
66+
->atPath('['.$field.']')
67+
->setParameter('{{ field }}', $field)
68+
->addViolation();
6769
}
6870
}
6971

7072
if (!$constraint->allowExtraFields) {
7173
foreach ($value as $field => $fieldValue) {
7274
if (!isset($constraint->fields[$field])) {
73-
$context->addViolationAt('['.$field.']', $constraint->extraFieldsMessage, array(
74-
'{{ field }}' => $field
75-
), $fieldValue);
75+
$context->buildViolation($constraint->extraFieldsMessage)
76+
->atPath('['.$field.']')
77+
->setParameter('{{ field }}', $field)
78+
->addViolation();
7679
}
7780
}
7881
}

src/Symfony/Component/Validator/Constraints/CountValidator.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,34 @@ public function validate($value, Constraint $constraint)
3636
$count = count($value);
3737

3838
if ($constraint->min == $constraint->max && $count != $constraint->min) {
39-
$this->context->addViolation($constraint->exactMessage, array(
40-
'{{ count }}' => $count,
41-
'{{ limit }}' => $constraint->min,
42-
), $value, (int) $constraint->min);
39+
$this->context->buildViolation($constraint->exactMessage)
40+
->setParameter('{{ count }}', $count)
41+
->setParameter('{{ limit }}', $constraint->min)
42+
->setValue($value)
43+
->setPlural((int) $constraint->min)
44+
->addViolation();
4345

4446
return;
4547
}
4648

4749
if (null !== $constraint->max && $count > $constraint->max) {
48-
$this->context->addViolation($constraint->maxMessage, array(
49-
'{{ count }}' => $count,
50-
'{{ limit }}' => $constraint->max,
51-
), $value, (int) $constraint->max);
50+
$this->context->buildViolation($constraint->maxMessage)
51+
->setParameter('{{ count }}', $count)
52+
->setParameter('{{ limit }}', $constraint->max)
53+
->setValue($value)
54+
->setPlural((int) $constraint->max)
55+
->addViolation();
5256

5357
return;
5458
}
5559

5660
if (null !== $constraint->min && $count < $constraint->min) {
57-
$this->context->addViolation($constraint->minMessage, array(
58-
'{{ count }}' => $count,
59-
'{{ limit }}' => $constraint->min,
60-
), $value, (int) $constraint->min);
61+
$this->context->buildViolation($constraint->minMessage)
62+
->setParameter('{{ count }}', $count)
63+
->setParameter('{{ limit }}', $constraint->min)
64+
->setValue($value)
65+
->setPlural((int) $constraint->min)
66+
->addViolation();
6167
}
6268
}
6369
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
18+
/**
19+
* @author Bernhard Schussek <bschussek@gmail.com>
20+
*
21+
* @deprecated Deprecated since version 2.5.3, to be removed in 3.0.
22+
*/
23+
class LegacyAllValidator extends ConstraintValidator
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function validate($value, Constraint $constraint)
29+
{
30+
if (!$constraint instanceof All) {
31+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\All');
32+
}
33+
34+
if (null === $value) {
35+
return;
36+
}
37+
38+
if (!is_array($value) && !$value instanceof \Traversable) {
39+
throw new UnexpectedTypeException($value, 'array or Traversable');
40+
}
41+
42+
$context = $this->context;
43+
$group = $context->getGroup();
44+
45+
foreach ($value as $key => $element) {
46+
foreach ($constraint->constraints as $constr) {
47+
$context->validateValue($element, $constr, '['.$key.']', $group);
48+
}
49+
}
50+
}
51+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
17+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
18+
19+
/**
20+
* ChoiceValidator validates that the value is one of the expected values.
21+
*
22+
* @author Fabien Potencier <fabien@symfony.com>
23+
* @author Florian Eckerstorfer <florian@eckerstorfer.org>
24+
* @author Bernhard Schussek <bschussek@gmail.com>
25+
*
26+
* @deprecated Deprecated since version 2.5.3, to be removed in 3.0.
27+
*/
28+
class LegacyChoiceValidator extends ConstraintValidator
29+
{
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function validate($value, Constraint $constraint)
34+
{
35+
if (!$constraint instanceof Choice) {
36+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Choice');
37+
}
38+
39+
if (!$constraint->choices && !$constraint->callback) {
40+
throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice');
41+
}
42+
43+
if (null === $value) {
44+
return;
45+
}
46+
47+
if ($constraint->multiple && !is_array($value)) {
48+
throw new UnexpectedTypeException($value, 'array');
49+
}
50+
51+
if ($constraint->callback) {
52+
if (is_callable(array($this->context->getClassName(), $constraint->callback))) {
53+
$choices = call_user_func(array($this->context->getClassName(), $constraint->callback));
54+
} elseif (is_callable($constraint->callback)) {
55+
$choices = call_user_func($constraint->callback);
56+
} else {
57+
throw new ConstraintDefinitionException('The Choice constraint expects a valid callback');
58+
}
59+
} else {
60+
$choices = $constraint->choices;
61+
}
62+
63+
if ($constraint->multiple) {
64+
foreach ($value as $_value) {
65+
if (!in_array($_value, $choices, $constraint->strict)) {
66+
$this->context->addViolation($constraint->multipleMessage, array('{{ value }}' => $_value));
67+
}
68+
}
69+
70+
$count = count($value);
71+
72+
if ($constraint->min !== null && $count < $constraint->min) {
73+
$this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min), null, (int) $constraint->min);
74+
75+
return;
76+
}
77+
78+
if ($constraint->max !== null && $count > $constraint->max) {
79+
$this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max), null, (int) $constraint->max);
80+
81+
return;
82+
}
83+
} elseif (!in_array($value, $choices, $constraint->strict)) {
84+
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
85+
}
86+
}
87+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
18+
/**
19+
* @author Bernhard Schussek <bschussek@gmail.com>
20+
*
21+
* @deprecated Deprecated since version 2.5.3, to be removed in 3.0.
22+
*/
23+
class LegacyCollectionValidator extends ConstraintValidator
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function validate($value, Constraint $constraint)
29+
{
30+
if (!$constraint instanceof Collection) {
31+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Collection');
32+
}
33+
34+
if (null === $value) {
35+
return;
36+
}
37+
38+
if (!is_array($value) && !($value instanceof \Traversable && $value instanceof \ArrayAccess)) {
39+
throw new UnexpectedTypeException($value, 'array or Traversable and ArrayAccess');
40+
}
41+
42+
// We need to keep the initialized context when CollectionValidator
43+
// calls itself recursively (Collection constraints can be nested).
44+
// Since the context of the validator is overwritten when initialize()
45+
// is called for the nested constraint, the outer validator is
46+
// acting on the wrong context when the nested validation terminates.
47+
//
48+
// A better solution - which should be approached in Symfony 3.0 - is to
49+
// remove the initialize() method and pass the context as last argument
50+
// to validate() instead.
51+
$context = $this->context;
52+
$group = $context->getGroup();
53+
54+
foreach ($constraint->fields as $field => $fieldConstraint) {
55+
if (
56+
// bug fix issue #2779
57+
(is_array($value) && array_key_exists($field, $value)) ||
58+
($value instanceof \ArrayAccess && $value->offsetExists($field))
59+
) {
60+
foreach ($fieldConstraint->constraints as $constr) {
61+
$context->validateValue($value[$field], $constr, '['.$field.']', $group);
62+
}
63+
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
64+
$context->addViolationAt('['.$field.']', $constraint->missingFieldsMessage, array(
65+
'{{ field }}' => $field
66+
), null);
67+
}
68+
}
69+
70+
if (!$constraint->allowExtraFields) {
71+
foreach ($value as $field => $fieldValue) {
72+
if (!isset($constraint->fields[$field])) {
73+
$context->addViolationAt('['.$field.']', $constraint->extraFieldsMessage, array(
74+
'{{ field }}' => $field
75+
), $fieldValue);
76+
}
77+
}
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)
0