Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit19d66ba

Browse files
committed
Add ability retrieve errors by their code.
1 parent983b560 commit19d66ba

File tree

4 files changed

+78
-2
lines changed

4 files changed

+78
-2
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
useSymfony\Component\Form\Exception\InvalidArgumentException;
1515
useSymfony\Component\Form\Exception\OutOfBoundsException;
1616
useSymfony\Component\Form\Exception\BadMethodCallException;
17+
useSymfony\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+
publicfunctiongetFormErrorByCode($code)
270+
{
271+
foreach ($thisas$formError) {
272+
$cause =$formError->getCause();
273+
if ($causeinstanceof ConstraintViolation &&$cause->getCode() ===$code) {
274+
return$formError;
275+
}
276+
}
277+
}
278+
268279
/**
269280
* Utility function for indenting multi-line strings.
270281
*
Lines 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+
namespaceSymfony\Component\Form\Tests;
13+
14+
useSymfony\Component\EventDispatcher\EventDispatcher;
15+
useSymfony\Component\Form\FormBuilder;
16+
useSymfony\Component\Form\FormError;
17+
useSymfony\Component\Validator\ConstraintViolation;
18+
19+
class FormErrorIteratorTestextends \PHPUnit_Framework_TestCase
20+
{
21+
publicfunctiontestGetFormErrorByCode()
22+
{
23+
if (!class_exists(ConstraintViolation::class)) {
24+
$this->markTestSkipped('Validator component required.');
25+
}
26+
27+
$formBuilder =newFormBuilder(
28+
'form',
29+
null,
30+
newEventDispatcher(),
31+
$this->getMock('Symfony\Component\Form\FormFactoryInterface'),
32+
array()
33+
);
34+
35+
$form =$formBuilder->getForm();
36+
37+
$code ='code';
38+
39+
$cause =newConstraintViolation('Error!',null,array(),null,'',null,null,$code);
40+
$form->addError(newFormError('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‎

Lines 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+
publicfunctiongetViolationByCode($code)
163+
{
164+
foreach ($thisas$violation) {
165+
if ($violation->getCode() ===$code) {
166+
return$violation;
167+
}
168+
}
169+
}
161170
}

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

Lines 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-
protectedfunctiongetViolation($message,$root =null,$propertyPath =null)
130+
publicfunctiontestGetViolationByCode()
131131
{
132-
returnnewConstraintViolation($message,$message,array(),$root,$propertyPath,null);
132+
$code ='code';
133+
$violation =$this->getViolation('Error',null,null,$code);
134+
$this->list =newConstraintViolationList(array($violation));
135+
136+
$this->assertInstanceOf(ConstraintViolation::class,$this->list->getViolationByCode($code));
137+
$this->assertNull($this->list->getViolationByCode('not-existent-code'));
138+
}
139+
140+
protectedfunctiongetViolation($message,$root =null,$propertyPath =null,$code =null)
141+
{
142+
returnnewConstraintViolation($message,$message,array(),$root,$propertyPath,null,null,$code);
133143
}
134144
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp