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 545dc12

Browse filesBrowse files
committed
Make the findByCodes able to filter FormErrors that have a cause that is string or object that has a __toString method
1 parent 944b06d commit 545dc12
Copy full SHA for 545dc12

File tree

3 files changed

+49
-1
lines changed
Filter options

3 files changed

+49
-1
lines changed

‎src/Symfony/Component/Form/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CHANGELOG
99
* deprecated calling `FormRenderer::searchAndRenderBlock` for fields which were already rendered
1010
* added a cause when a CSRF error has occurred
1111
* deprecated the `scale` option of the `IntegerType`
12+
* findByCodes of `FormErrorIterator` is able to also filter causes that are string or objects that implement the `__toString` method
1213

1314
4.1.0
1415
-----

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/FormErrorIterator.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,16 @@ public function findByCodes($codes)
267267
$cause = $error->getCause();
268268
if ($cause instanceof ConstraintViolation && \in_array($cause->getCode(), $codes, true)) {
269269
$errors[] = $error;
270+
continue;
271+
}
272+
273+
if (!is_scalar($cause) && !(\is_object($cause) && method_exists($cause, '__toString'))) {
274+
continue;
275+
}
276+
277+
$cause = (string) $cause;
278+
if (\in_array($cause, $codes, true)) {
279+
$errors[] = $error;
270280
}
271281
}
272282

‎src/Symfony/Component/Form/Tests/FormErrorIteratorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/FormErrorIteratorTest.php
+38-1Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class FormErrorIteratorTest extends TestCase
2323
/**
2424
* @dataProvider findByCodesProvider
2525
*/
26-
public function testFindByCodes($code, $violationsCount)
26+
public function testFindByCodesWhenAllCausesAreViolationConstraints($code, $violationsCount)
2727
{
2828
if (!class_exists(ConstraintViolation::class)) {
2929
$this->markTestSkipped('Validator component required.');
@@ -52,6 +52,43 @@ public function testFindByCodes($code, $violationsCount)
5252
$this->assertCount($violationsCount, $specificFormErrors);
5353
}
5454

55+
/**
56+
* @dataProvider findByCodesProvider
57+
*/
58+
public function testFindByCodesWhenAllCausesAreVariousObjectsOrSimpleStrings($code, $violationsCount)
59+
{
60+
if (!class_exists(ConstraintViolation::class)) {
61+
$this->markTestSkipped('Validator component required.');
62+
}
63+
64+
$formBuilder = new FormBuilder(
65+
'form',
66+
null,
67+
new EventDispatcher(),
68+
$this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock(),
69+
array()
70+
);
71+
72+
$form = $formBuilder->getForm();
73+
74+
$cause = new ConstraintViolation('Error 1!', null, array(), null, '', null, null, 'code1');
75+
$form->addError(new FormError('Error 1!', null, array(), null, $cause));
76+
$cause = new class() {
77+
public function __toString()
78+
{
79+
return 'code1';
80+
}
81+
};
82+
83+
$form->addError(new FormError('Error 2!', null, array(), null, $cause));
84+
$form->addError(new FormError('Error 3!', null, array(), null, 'code2'));
85+
$formErrors = $form->getErrors();
86+
87+
$specificFormErrors = $formErrors->findByCodes($code);
88+
$this->assertInstanceOf(FormErrorIterator::class, $specificFormErrors);
89+
$this->assertCount($violationsCount, $specificFormErrors);
90+
}
91+
5592
public function findByCodesProvider()
5693
{
5794
return array(

0 commit comments

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