Skip to content

Commit 08172bf

Browse files
committed
[Validator] Merged validate(), validateObject() and validateObjects() to simplify usage
1 parent 51197f6 commit 08172bf

File tree

10 files changed

+93
-239
lines changed

10 files changed

+93
-239
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Valid extends Constraint
2525
{
2626
public $traverse = true;
2727

28-
public $deep = false;
28+
public $deep = true;
2929

3030
public function __construct($options = null)
3131
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function validate($value, $subPath = '', $groups = null, $traverse = fals
143143
->getValidator()
144144
->inContext($this)
145145
->atPath($subPath)
146-
->validateObject($value, $groups)
146+
->validate($value, null, $groups)
147147
;
148148
}
149149

src/Symfony/Component/Validator/Tests/Validator/Abstract2Dot5ApiTest.php

Lines changed: 13 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,11 @@ protected function setUp()
5050
$this->validator = $this->createValidator($this->metadataFactory);
5151
}
5252

53-
protected function validate($value, $constraints, $groups = null)
53+
protected function validate($value, $constraints = null, $groups = null)
5454
{
5555
return $this->validator->validate($value, $constraints, $groups);
5656
}
5757

58-
protected function validateObject($object, $groups = null)
59-
{
60-
return $this->validator->validateObject($object, $groups);
61-
}
62-
63-
protected function validateObjects($objects, $groups = null, $deep = false)
64-
{
65-
return $this->validator->validateObjects($objects, $groups, $deep);
66-
}
67-
6858
protected function validateProperty($object, $propertyName, $groups = null)
6959
{
7060
return $this->validator->validateProperty($object, $propertyName, $groups);
@@ -88,7 +78,7 @@ public function testNoDuplicateValidationIfConstraintInMultipleGroups()
8878
'groups' => array('Group 1', 'Group 2'),
8979
)));
9080

91-
$violations = $this->validator->validateObject($entity, array('Group 1', 'Group 2'));
81+
$violations = $this->validator->validate($entity, new Valid(), array('Group 1', 'Group 2'));
9282

9383
/** @var ConstraintViolationInterface[] $violations */
9484
$this->assertCount(1, $violations);
@@ -119,7 +109,7 @@ public function testGroupSequenceAbortsAfterFailedGroup()
119109
)));
120110

121111
$sequence = new GroupSequence(array('Group 1', 'Group 2', 'Group 3'));
122-
$violations = $this->validator->validateObject($entity, $sequence);
112+
$violations = $this->validator->validate($entity, new Valid(), $sequence);
123113

124114
/** @var ConstraintViolationInterface[] $violations */
125115
$this->assertCount(1, $violations);
@@ -149,7 +139,7 @@ public function testGroupSequenceIncludesReferences()
149139
)));
150140

151141
$sequence = new GroupSequence(array('Group 1', 'Entity'));
152-
$violations = $this->validator->validateObject($entity, $sequence);
142+
$violations = $this->validator->validate($entity, new Valid(), $sequence);
153143

154144
/** @var ConstraintViolationInterface[] $violations */
155145
$this->assertCount(1, $violations);
@@ -167,7 +157,7 @@ public function testValidateInSeparateContext()
167157
->getValidator()
168158
// Since the validator is not context aware, the group must
169159
// be passed explicitly
170-
->validateObject($value->reference, 'Group')
160+
->validate($value->reference, new Valid(), 'Group')
171161
;
172162

173163
/** @var ConstraintViolationInterface[] $violations */
@@ -208,7 +198,7 @@ public function testValidateInSeparateContext()
208198
'groups' => 'Group',
209199
)));
210200

211-
$violations = $this->validator->validateObject($entity, 'Group');
201+
$violations = $this->validator->validate($entity, new Valid(), 'Group');
212202

213203
/** @var ConstraintViolationInterface[] $violations */
214204
$this->assertCount(1, $violations);
@@ -226,7 +216,7 @@ public function testValidateInContext()
226216
->getValidator()
227217
->inContext($context)
228218
->atPath('subpath')
229-
->validateObject($value->reference)
219+
->validate($value->reference)
230220
;
231221
};
232222

@@ -252,7 +242,7 @@ public function testValidateInContext()
252242
'groups' => 'Group',
253243
)));
254244

255-
$violations = $this->validator->validateObject($entity, 'Group');
245+
$violations = $this->validator->validate($entity, new Valid(), 'Group');
256246

257247
/** @var ConstraintViolationInterface[] $violations */
258248
$this->assertCount(1, $violations);
@@ -277,7 +267,7 @@ public function testValidateArrayInContext()
277267
->getValidator()
278268
->inContext($context)
279269
->atPath('subpath')
280-
->validateObjects(array('key' => $value->reference))
270+
->validate(array('key' => $value->reference))
281271
;
282272
};
283273

@@ -303,7 +293,7 @@ public function testValidateArrayInContext()
303293
'groups' => 'Group',
304294
)));
305295

306-
$violations = $this->validator->validateObject($entity, 'Group');
296+
$violations = $this->validator->validate($entity, new Valid(), 'Group');
307297

308298
/** @var ConstraintViolationInterface[] $violations */
309299
$this->assertCount(1, $violations);
@@ -317,44 +307,6 @@ public function testValidateArrayInContext()
317307
$this->assertNull($violations[0]->getCode());
318308
}
319309

320-
public function testValidateAcceptsValid()
321-
{
322-
$test = $this;
323-
$entity = new Entity();
324-
325-
$callback = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
326-
$test->assertSame($test::ENTITY_CLASS, $context->getClassName());
327-
$test->assertNull($context->getPropertyName());
328-
$test->assertSame('', $context->getPropertyPath());
329-
$test->assertSame('Group', $context->getGroup());
330-
$test->assertSame($test->metadata, $context->getMetadata());
331-
$test->assertSame($entity, $context->getRoot());
332-
$test->assertSame($entity, $context->getValue());
333-
$test->assertSame($entity, $value);
334-
335-
$context->addViolation('Message %param%', array('%param%' => 'value'));
336-
};
337-
338-
$this->metadata->addConstraint(new Callback(array(
339-
'callback' => $callback,
340-
'groups' => 'Group',
341-
)));
342-
343-
// This is the same as when calling validateObject()
344-
$violations = $this->validator->validate($entity, new Valid(), 'Group');
345-
346-
/** @var ConstraintViolationInterface[] $violations */
347-
$this->assertCount(1, $violations);
348-
$this->assertSame('Message value', $violations[0]->getMessage());
349-
$this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
350-
$this->assertSame(array('%param%' => 'value'), $violations[0]->getMessageParameters());
351-
$this->assertSame('', $violations[0]->getPropertyPath());
352-
$this->assertSame($entity, $violations[0]->getRoot());
353-
$this->assertSame($entity, $violations[0]->getInvalidValue());
354-
$this->assertNull($violations[0]->getMessagePluralization());
355-
$this->assertNull($violations[0]->getCode());
356-
}
357-
358310
public function testTraverseTraversableByDefault()
359311
{
360312
$test = $this;
@@ -380,7 +332,7 @@ public function testTraverseTraversableByDefault()
380332
'groups' => 'Group',
381333
)));
382334

383-
$violations = $this->validateObject($traversable, 'Group');
335+
$violations = $this->validate($traversable, new Valid(), 'Group');
384336

385337
/** @var ConstraintViolationInterface[] $violations */
386338
$this->assertCount(1, $violations);
@@ -403,7 +355,7 @@ public function testExpectTraversableIfTraverseOnClass()
403355

404356
$this->metadata->addConstraint(new Traverse());
405357

406-
$this->validator->validateObject($entity);
358+
$this->validator->validate($entity);
407359
}
408360

409361
public function testAddCustomizedViolation()
@@ -421,7 +373,7 @@ public function testAddCustomizedViolation()
421373

422374
$this->metadata->addConstraint(new Callback($callback));
423375

424-
$violations = $this->validator->validateObject($entity);
376+
$violations = $this->validator->validate($entity);
425377

426378
/** @var ConstraintViolationInterface[] $violations */
427379
$this->assertCount(1, $violations);

src/Symfony/Component/Validator/Tests/Validator/AbstractLegacyApiTest.php

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

1212
namespace Symfony\Component\Validator\Tests\Validator;
1313

14+
use Symfony\Component\Validator\Constraint;
1415
use Symfony\Component\Validator\Constraints\Callback;
1516
use Symfony\Component\Validator\Constraints\Valid;
1617
use Symfony\Component\Validator\ConstraintViolationInterface;
@@ -47,19 +48,17 @@ protected function setUp()
4748
$this->validator = $this->createValidator($this->metadataFactory);
4849
}
4950

50-
protected function validate($value, $constraints, $groups = null)
51+
protected function validate($value, $constraints = null, $groups = null)
5152
{
52-
return $this->validator->validateValue($value, $constraints, $groups);
53-
}
53+
if (null === $constraints) {
54+
$constraints = new Valid();
55+
}
5456

55-
protected function validateObject($object, $groups = null)
56-
{
57-
return $this->validator->validate($object, $groups);
58-
}
57+
if ($constraints instanceof Valid) {
58+
return $this->validator->validate($value, $groups, $constraints->traverse, $constraints->deep);
59+
}
5960

60-
protected function validateObjects($objects, $groups = null, $deep = false)
61-
{
62-
return $this->validator->validate($objects, $groups, true, $deep);
61+
return $this->validator->validateValue($value, $constraints, $groups);
6362
}
6463

6564
protected function validateProperty($object, $propertyName, $groups = null)
@@ -226,7 +225,7 @@ public function testAddCustomizedViolation()
226225

227226
$this->metadata->addConstraint(new Callback($callback));
228227

229-
$violations = $this->validateObject($entity);
228+
$violations = $this->validator->validate($entity);
230229

231230
/** @var ConstraintViolationInterface[] $violations */
232231
$this->assertCount(1, $violations);

0 commit comments

Comments
 (0)