Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6641f3e

Browse files
committed
[Validator] Added constraints Optional and Required for the CollectionValidator
1 parent efada56 commit 6641f3e

File tree

4 files changed

+200
-2
lines changed

4 files changed

+200
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,18 @@ public function isValid($value, Constraint $constraint)
5151
$extraFields[$field] = $fieldValue;
5252
}
5353

54-
foreach ($constraint->fields as $field => $constraints) {
54+
foreach ($constraint->fields as $field => $fieldConstraint) {
5555
if (
5656
// bug fix issue #2779
5757
(is_array($value) && array_key_exists($field, $value)) ||
5858
($value instanceof \ArrayAccess && $value->offsetExists($field))
5959
) {
60+
if ($fieldConstraint instanceof Required || $fieldConstraint instanceof Optional) {
61+
$constraints = $fieldConstraint->constraints;
62+
} else {
63+
$constraints = $fieldConstraint;
64+
}
65+
6066
// cannot simply cast to array, because then the object is converted to an
6167
// array instead of wrapped inside
6268
$constraints = is_array($constraints) ? $constraints : array($constraints);
@@ -66,7 +72,7 @@ public function isValid($value, Constraint $constraint)
6672
}
6773

6874
unset($extraFields[$field]);
69-
} else {
75+
} else if (!$fieldConstraint instanceof Optional) {
7076
$missingFields[] = $field;
7177
}
7278
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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+
16+
/**
17+
* @Annotation
18+
*
19+
* @api
20+
*/
21+
class Optional extends Constraint
22+
{
23+
public $constraints = array();
24+
25+
public function getDefaultOption()
26+
{
27+
return 'constraints';
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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+
16+
/**
17+
* @Annotation
18+
*
19+
* @api
20+
*/
21+
class Required extends Constraint
22+
{
23+
public $constraints = array();
24+
25+
public function getDefaultOption()
26+
{
27+
return 'constraints';
28+
}
29+
}

tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorTest.php

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Symfony\Component\Validator\Constraints\Min;
1616
use Symfony\Component\Validator\Constraints\NotBlank;
1717
use Symfony\Component\Validator\Constraints\NotNull;
18+
use Symfony\Component\Validator\Constraints\Required;
19+
use Symfony\Component\Validator\Constraints\Optional;
1820
use Symfony\Component\Validator\Constraints\Collection;
1921
use Symfony\Component\Validator\Constraints\CollectionValidator;
2022

@@ -309,6 +311,138 @@ public function testArrayObject() {
309311
$this->assertTrue($result);
310312
}
311313

314+
public function testOptionalFieldPresent()
315+
{
316+
$array = array(
317+
'foo' => null,
318+
);
319+
320+
$this->assertTrue($this->validator->isValid($array, new Collection(array(
321+
'foo' => new Optional(),
322+
))));
323+
}
324+
325+
public function testOptionalFieldNotPresent()
326+
{
327+
$array = array(
328+
);
329+
330+
$this->assertTrue($this->validator->isValid($array, new Collection(array(
331+
'foo' => new Optional(),
332+
))));
333+
}
334+
335+
public function testOptionalFieldSingleConstraint()
336+
{
337+
$this->context->setGroup('MyGroup');
338+
$this->context->setPropertyPath('bar');
339+
340+
$array = array(
341+
'foo' => 5,
342+
);
343+
344+
$constraint = new Min(4);
345+
346+
$this->walker->expects($this->once())
347+
->method('walkConstraint')
348+
->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]'));
349+
350+
$this->assertTrue($this->validator->isValid($array, new Collection(array(
351+
'foo' => new Optional($constraint),
352+
))));
353+
}
354+
355+
public function testOptionalFieldMultipleConstraints()
356+
{
357+
$this->context->setGroup('MyGroup');
358+
$this->context->setPropertyPath('bar');
359+
360+
$array = array(
361+
'foo' => 5,
362+
);
363+
364+
$constraints = array(
365+
new NotNull(),
366+
new Min(4),
367+
);
368+
369+
foreach ($constraints as $i => $constraint) {
370+
$this->walker->expects($this->at($i))
371+
->method('walkConstraint')
372+
->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]'));
373+
}
374+
375+
$this->assertTrue($this->validator->isValid($array, new Collection(array(
376+
'foo' => new Optional($constraints),
377+
))));
378+
}
379+
380+
public function testRequiredFieldPresent()
381+
{
382+
$array = array(
383+
'foo' => null,
384+
);
385+
386+
$this->assertTrue($this->validator->isValid($array, new Collection(array(
387+
'foo' => new Required(),
388+
))));
389+
}
390+
391+
public function testRequiredFieldNotPresent()
392+
{
393+
$array = array(
394+
);
395+
396+
$this->assertFalse($this->validator->isValid($array, new Collection(array(
397+
'foo' => new Required(),
398+
))));
399+
}
400+
401+
public function testRequiredFieldSingleConstraint()
402+
{
403+
$this->context->setGroup('MyGroup');
404+
$this->context->setPropertyPath('bar');
405+
406+
$array = array(
407+
'foo' => 5,
408+
);
409+
410+
$constraint = new Min(4);
411+
412+
$this->walker->expects($this->once())
413+
->method('walkConstraint')
414+
->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]'));
415+
416+
$this->assertTrue($this->validator->isValid($array, new Collection(array(
417+
'foo' => new Required($constraint),
418+
))));
419+
}
420+
421+
public function testRequiredFieldMultipleConstraints()
422+
{
423+
$this->context->setGroup('MyGroup');
424+
$this->context->setPropertyPath('bar');
425+
426+
$array = array(
427+
'foo' => 5,
428+
);
429+
430+
$constraints = array(
431+
new NotNull(),
432+
new Min(4),
433+
);
434+
435+
foreach ($constraints as $i => $constraint) {
436+
$this->walker->expects($this->at($i))
437+
->method('walkConstraint')
438+
->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]'));
439+
}
440+
441+
$this->assertTrue($this->validator->isValid($array, new Collection(array(
442+
'foo' => new Required($constraints),
443+
))));
444+
}
445+
312446
public function testObjectShouldBeLeftUnchanged()
313447
{
314448
$value = new \ArrayObject(array(

0 commit comments

Comments
 (0)