8000 [Validator] Changed validateValue() to validate() in the new API · symfony/symfony@718601c · GitHub
[go: up one dir, main page]

Skip to content

Commit 718601c

Browse files
committed
[Validator] Changed validateValue() to validate() in the new API
The validation of values against constraints should be a first-class citizen in the API. For this reason, validate() now takes a value and a constraint or a list of constraints. This method should be used for the most basic use cases. If users want to annotate objects with constraints (this is optional, advanced functionality), they can use the more expressive validateObject() method now. For traversing arrays or Traversables, a new method validateCollection() is now available in the API.
1 parent ee1adad commit 718601c

File tree

5 files changed

+52
-42
lines changed

5 files changed

+52
-42
lines changed

src/Symfony/Component/Validator/Context/LegacyExecutionContext.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function validate($value, $subPath = '', $groups = null, $traverse = fals
112112
->getValidator()
113113
->inContext($this)
114114
->atPath($subPath)
115-
->validateValue($value, $constraint, $groups)
115+
->validate($value, $constraint, $groups)
116116
;
117117
}
118118

@@ -126,7 +126,7 @@ public function validate($value, $subPath = '', $groups = null, $traverse = fals
126126
->getValidator()
127127
->inContext($this)
128128
->atPath($subPath)
129-
->validateValue($value, $constraints, $groups)
129+
->validate($value, $constraints, $groups)
130130
;
131131
}
132132

@@ -147,7 +147,7 @@ public function validateValue($value, $constraints, $subPath = '', $groups = nul
147147
->getValidator()
148148
->inContext($this)
149149
->atPath($subPath)
150-
->validateValue($value, $constraints, $groups)
150+
->validate($value, $constraints, $groups)
151151
;
152152
}
153153

src/Symfony/Component/Validator/Validator/ContextualValidator.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ public function atPath($subPath)
4646
return $this;
4747
}
4848

49+
/**
50+
* Validates a value against a constraint or a list of constraints.
51+
*
52+
* @param mixed $value The value to validate.
53+
* @param Constraint|Constraint[] $constraints The constraint(s) to validate against.
54+
* @param array|null $groups The validation groups to validate.
55+
*
56+
* @return ConstraintViolationListInterface A list of constraint violations. If the
57+
* list is empty, validation succeeded.
58+
*/
59+
public function validate($value, $constraints, $groups = null)
60+
{
61+
$this->traverseValue($value, $constraints, $groups);
62+
63+
return $this->context->getViolations();
64+
}
65+
4966
/**
5067
* Validates a value.
5168
*
@@ -118,21 +135,4 @@ public function validatePropertyValue($object, $propertyName, $value, $groups =
118135

119136
return $this->context->getViolations();
120137
}
121-
122-
/**
123-
* Validates a value against a constraint or a list of constraints.
124-
*
125-
* @param mixed $value The value to validate.
126-
* @param Constraint|Constraint[] $constraints The constraint(s) to validate against.
127-
* @param array|null $groups The validation groups to validate.
128-
*
129-
* @return ConstraintViolationListInterface A list of constraint violations. If the
130-
* list is empty, validation succeeded.
131-
*/
132-
public function validateValue($value, $constraints, $groups = null)
133-
{
134-
$this->traverseValue($value, $constraints, $groups);
135-
136-
return $this->context->getViolations();
137-
}
138138
}

src/Symfony/Component/Validator/Validator/LegacyValidator.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Validator;
1313

14+
use Symfony\Component\Validator\Constraint;
1415
use Symfony\Component\Validator\Constraints\Traverse;
1516
use Symfony\Component\Validator\Constraints\Valid;
1617
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
@@ -23,6 +24,11 @@ class LegacyValidator extends Validator implements LegacyValidatorInterface
2324
{
2425
public function validate($value, $groups = null, $traverse = false, $deep = false)
2526
{
27+
// Use new signature if constraints are given in the second argument
28+
if (func_num_args() <= 3 && ($groups instanceof Constraint || (is_array($groups) && current($groups) instanceof Constraint))) {
29+
return parent::validate($value, $groups, $traverse);
30+
}
31+
2632
if (is_array($value)) {
2733
$constraint = new Traverse(array(
2834
'traverse' => true,
@@ -44,6 +50,11 @@ public function validate($value, $groups = null, $traverse = false, $deep = fals
4450
return $this->validateObject($value, $groups);
4551
}
4652

53+
public function validateValue($value, $constraints, $groups = null)
54+
{
55+
return parent::validate($value, $constraints, $groups);
56+
}
57+
4758
public function getMetadataFactory()
4859
{
4960
return $this->metadataFactory;

src/Symfony/Component/Validator/Validator/Validator.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ public function __construct(NodeTraverserInterface $nodeTraverser, MetadataFacto
3434
$this->contextManager = $contextManager;
3535
}
3636

37+
public function validate($value, $constraints, $groups = null)
38+
{
39+
$this->contextManager->startContext($value);
40+
41+
$this->traverseValue($value, $constraints, $groups);
42+
43+
return $this->contextManager->stopContext()->getViolations();
44+
}
45+
3746
public function validateObject($object, $groups = null)
3847
{
3948
$this->contextManager->startContext($object);
@@ -74,14 +83,4 @@ public function validatePropertyValue($object, $propertyName, $value, $groups =
7483

7584
return $this->contextManager->stopContext()->getViolations();
7685
}
77-
78-
public function validateValue($value, $constraints, $groups = null)
79-
{
80-
$this->contextManager->startContext($value);
81-
82-
$this->traverseValue($value, $constraints, $groups);
83-
84-
return $this->contextManager->stopContext()->getViolations();
85-
}
86-
8786
}

src/Symfony/Component/Validator/Validator/ValidatorInterface.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@
2121
*/
2222
interface ValidatorInterface
2323
{
24+
/**
25+
* Validates a value against a constraint or a list of constraints.
26+
*
27+
* @param mixed $value The value to validate.
28+
* @param Constraint|Constraint[] $constraints The constraint(s) to validate against.
29+
* @param array|null $groups The validation groups to validate.
30+
*
31+
* @return ConstraintViolationListInterface A list of constraint violations. If the
32+
* list is empty, validation succeeded.
33+
*/
34+
public function validate($value, $constraints, $groups = null);
35+
2436
/**
2537
* Validates a value.
2638
*
@@ -69,18 +81,6 @@ public function validateProperty($object, $propertyName, $groups = null);
6981
*/
7082
public function validatePropertyValue($object, $propertyName, $value, $groups = null);
7183

72-
/**
73-
* Validates a value against a constraint or a list of constraints.
74-
*
75-
* @param mixed $value The value to validate.
76-
* @param Constraint|Constraint[] $constraints The constraint(s) to validate against.
77-
* @param array|null $groups The validation groups to validate.
78-
*
79-
* @return ConstraintViolationListInterface A list of constraint violations. If the
80-
* list is empty, validation succeeded.
81-
*/
82-
public function validateValue($value, $constraints, $groups = null);
83-
8484
/**
8585
* @param ExecutionContextInterface $context
8686
*

0 commit comments

Comments
 (0)
0