Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 19d66ba

Browse filesBrowse files
committed
Add ability retrieve errors by their code.
1 parent 983b560 commit 19d66ba
Copy full SHA for 19d66ba

File tree

4 files changed

+78
-2
lines changed
Filter options

4 files changed

+78
-2
lines changed

‎src/Symfony/Component/Form/FormErrorIterator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/FormErrorIterator.php
+11Lines changed: 11 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,16 @@ public function seek($position)
265266
}
266267
}
267268

269+
public function getFormErrorByCode($code)
270+
{
271+
foreach ($this as $formError) {
272+
$cause = $formError->getCause();
273+
if ($cause instanceof ConstraintViolation && $cause->getCode() === $code) {
274+
return $formError;
275+
}
276+
}
277+
}
278+
268279
/**
269280
* Utility function for indenting multi-line strings.
270281
*
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\Validator\ConstraintViolation;
18+
19+
class FormErrorIteratorTest extends \PHPUnit_Framework_TestCase
20+
{
21+
public function testGetFormErrorByCode()
22+
{
23+
if (!class_exists(ConstraintViolation::class)) {
24+
$this->markTestSkipped('Validator component required.');
25+
}
26+
27+
$formBuilder = new FormBuilder(
28+
'form',
29+
null,
30+
new EventDispatcher(),
31+
$this->getMock('Symfony\Component\Form\FormFactoryInterface'),
32+
array()
33+
);
34+
35+
$form = $formBuilder->getForm();
36+
37+
$code = 'code';
38+
39+
$cause = new ConstraintViolation('Error!', null, array(), null, '', null, null, $code);
40+
$form->addError(new FormError('Error!', null, array(), null, $cause));
41+
$formErrors = $form->getErrors();
42+
43+
$this->assertInstanceOf(FormError::class, $formErrors->getFormErrorByCode($code));
44+
$this->assertNull($formErrors->getFormErrorByCode('not-existent-code'));
45+
}
46+
}

‎src/Symfony/Component/Validator/ConstraintViolationList.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/ConstraintViolationList.php
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,13 @@ public function offsetUnset($offset)
158158
{
159159
$this->remove($offset);
160160
}
161+
162+
public function getViolationByCode($code)
163+
{
164+
foreach ($this as $violation) {
165+
if ($violation->getCode() === $code) {
166+
return $violation;
167+
}
168+
}
169+
}
161170
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/ConstraintViolationListTest.php
+12-2Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,18 @@ public function testToString()
127127
$this->assertEquals($expected, (string) $this->list);
128128
}
129129

130-
protected function getViolation($message, $root = null, $propertyPath = null)
130+
public function testGetViolationByCode()
131131
{
132-
return new ConstraintViolation($message, $message, array(), $root, $propertyPath, null);
132+
$code = 'code';
133+
$violation = $this->getViolation('Error', null, null, $code);
134+
$this->list = new ConstraintViolationList(array($violation));
135+
136+
$this->assertInstanceOf(ConstraintViolation::class, $this->list->getViolationByCode($code));
137+
$this->assertNull($this->list->getViolationByCode('not-existent-code'));
138+
}
139+
140+
protected function getViolation($message, $root = null, $propertyPath = null, $code = null)
141+
{
142+
return new ConstraintViolation($message, $message, array(), $root, $propertyPath, null, null, $code);
133143
}
134144
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.