8000 Add ability retrieve errors by their code. · symfony/symfony@c2e1323 · GitHub
[go: up one dir, main page]

Skip to content

Commit c2e1323

Browse files
committed
Add ability retrieve errors by their code.
1 parent 983b560 commit c2e1323

File tree

4 files changed

+131
-2
lines changed

4 files changed

+131
-2
lines changed

src/Symfony/Component/Form/FormErrorIterator.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Form\Exception\InvalidArgumentException;
1515
use Symfony\Component\Form\Exception\OutOfBoundsException;
1616
use Symfony\Component\Form\Exception\BadMethodCallException;
17+
use Symfony\Component\Validator\ConstraintViolation;
1718

1819
/**
1920
* Iterates over the errors of a form.
@@ -265,6 +266,26 @@ public function seek($position)
265266
}
266267
}
267268

269+
/**
270+
* Creates iterator for errors with specific codes.
271+
*
272+
* @param string|string[] $codes The codes to find
273+
* @return static New instance which contains only specific errors.
274+
*/
275+
public function findByCodes($codes)
276+
{
277+
$codes = (array)$codes;
278+
$formErrors = array();
279+
foreach ($this as $formError) {
280+
$cause = $formError->getCause();
281+
if ($cause instanceof ConstraintViolation && in_array($cause->getCode(), $codes, true)) {
282+
$formErrors[] = $formError;
283+
}
284+
}
285+
286+
return new static($this->form, $formErrors);
287+
}
288+
268289
/**
269290
* Utility function for indenting multi-line strings.
270291
*
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\Form\Tests;
13+
14+
use Symfony\Component\EventDispatcher\EventDispatcher;
15+
use Symfony\Component\Form\FormBuilder;
16+
use Symfony\Component\Form\FormError;
17+
use Symfony\Component\Form\FormErrorIterator;
18+
use Symfony\Component\Validator\ConstraintViolation;
19+
20+
class FormErrorIteratorTest extends \PHPUnit_Framework_TestCase
21+
{
22+
/**
23+
* @dataProvider findByCodesProvider
24+
*/
25+
public function testFindByCodes($code, $violationsCount)
26+
{
27+
if (!class_exists(ConstraintViolation::class)) {
28+
$this->markTestSkipped('Validator component required.');
29+
}
30+
31+
$formBuilder = new FormBuilder(
32+
'form',
33+
null,
34+
new EventDispatcher(),
35+
$this->getMock('Symfony\Component\Form\FormFactoryInterface'),
36+
array()
37+
);
38+
39+
$form = $formBuilder->getForm();
40+
41+
$cause = new ConstraintViolation('Error 1!', null, array(), null, '', null, null, 'code1');
42+
$form->addError(new FormError('Error 1!', null, array(), null, $cause));
43+
$cause = new ConstraintViolation('Error 2!', null, array(), null, '', null, null, 'code1');
44+
$form->addError(new FormError('Error 2!', null, array(), null, $cause));
45+
$cause = new ConstraintViolation('Error 3!', null, array(), null, '', null, null, 'code2');
46+
$form->addError(new FormError('Error 3!', null, array(), null, $cause));
47+
$formErrors = $form->getErrors();
48+
49+
$specificFormErrors = $formErrors->findByCodes($code);
50+
$this->assertInstanceOf(FormErrorIterator::class, $specificFormErrors);
51+
$this->assertCount($violationsCount, $specificFormErrors);
52+
}
53+
54+
public function findByCodesProvider()
55+
{
56+
return array(
57+
array('code1', 2),
58+
array(array('code1', 'code2'), 3),
59+
array('code3', 0),
60+
);
61+
}
62+
}

src/Symfony/Component/Validator/ConstraintViolationList.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,23 @@ public function offsetUnset($offset)
158158
{
159159
$this->remove($offset);
160160
}
161+
162+
/**
163+
* Creates iterator for errors with specific codes.
164+
*
165+
* @param string|string[] $codes The codes to find
166+
* @return static New instance which contains only specific errors.
167+
*/
168+
public function findByCodes($codes)
169+
{
170+
$codes = (array)$codes;
171+
$violations = array();
172+
foreach ($this as $violation) {
173+
if (in_array($violation->getCode(), $codes, true)) {
174+
$violations[] = $violation;
175+
}
176+
}
177+
178+
return new static($violations);
179+
}
161180
}

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,35 @@ public function testToString()
127127
$this->assertEquals($expected, (string) $this->list);
128128
}
129129

130-
protected function getViolation($message, $root = null, $propertyPath = null)
130+
/**
131+
* @dataProvider findByCodesProvider
132+
*/
133+
public function testFindByCodes($code, $violationsCount)
131134
{
132-
return new ConstraintViolation($message, $message, array(), $root, $propertyPath, null);
135+
$violations = array(
136+
$this->getViolation('Error', null, null, 'code1'),
137+
$this->getViolation('Error', null, null, 'code1'),
138+
$this->getViolation('Error', null, null, 'code2'),
139+
);
140+
$this->list = new ConstraintViolationList($violations);
141+
142+
$specificErrors = $this->list->findByCodes($code);
143+
144+
$this->assertInstanceOf(ConstraintViolation::class, $specificErrors);
145+
$this->assertCount($violationsCount, $specificErrors);
146+
}
147+
148+
public function findByCodesProvider()
149+
{
150+
return array(
151+
array('code1', 2),
152+
array(array('code1', 'code2'), 3),
153+
array('code3', 0),
154+
);
155+
}
156+
157+
protected function getViolation($message, $root = null, $propertyPath = null, $code = null)
158+
{
159+
return new ConstraintViolation($message, $message, array(), $root, $propertyPath, null, null, $code);
133160
}
134161
}

0 commit comments

Comments
 (0)
0