Skip to content

[Form] FormErrorIterator do not filter only with Constraint causes #28649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ CHANGELOG
* deprecated calling `FormRenderer::searchAndRenderBlock` for fields which were already rendered
* added a cause when a CSRF error has occurred
* deprecated the `scale` option of the `IntegerType`
* findByCodes of `FormErrorIterator` is able to also filter causes that are string or objects that implement the `__toString` method
* removed restriction on allowed HTTP methods

4.1.0
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Form/FormErrorIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,16 @@ public function findByCodes($codes)
$cause = $error->getCause();
if ($cause instanceof ConstraintViolation && \in_array($cause->getCode(), $codes, true)) {
$errors[] = $error;
continue;
}

if (!is_scalar($cause) && !(\is_object($cause) && method_exists($cause, '__toString'))) {
continue;
}

$cause = (string) $cause;
if (\in_array($cause, $codes, true)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id say in_array((string) $cause, ..

$errors[] = $error;
}
}

Expand Down
39 changes: 38 additions & 1 deletion src/Symfony/Component/Form/Tests/FormErrorIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FormErrorIteratorTest extends TestCase
/**
* @dataProvider findByCodesProvider
*/
public function testFindByCodes($code, $violationsCount)
public function testFindByCodesWhenAllCausesAreViolationConstraints($code, $violationsCount)
{
if (!class_exists(ConstraintViolation::class)) {
$this->markTestSkipped('Validator component required.');
Expand Down Expand Up @@ -52,6 +52,43 @@ public function testFindByCodes($code, $violationsCount)
$this->assertCount($violationsCount, $specificFormErrors);
}

/**
* @dataProvider findByCodesProvider
*/
public function testFindByCodesWhenAllCausesAreVariousObjectsOrSimpleStrings($code, $violationsCount)
{
if (!class_exists(ConstraintViolation::class)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not require validator component to test the new feature (we aim for compatibility without it)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI this test class on master contains one test case. I have duplicated this test case on the current PR. These lines are the copied code from the previous test case.

Your proposal is to remove them from both test cases?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI this test class on master contains one test case

then i suggest to refactor it a bit, and keep a single testFindByCodes() where the expectations are built dynamically (if validator exists yes/no)

$this->markTestSkipped('Validator component required.');
}

$formBuilder = new FormBuilder(
'form',
null,
new EventDispatcher(),
$this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock(),
array()
);

$form = $formBuilder->getForm();

$cause = new ConstraintViolation('Error 1!', null, array(), null, '', null, null, 'code1');
$form->addError(new FormError('Error 1!', null, array(), null, $cause));
$cause = new class() {
public function __toString()
{
return 'code1';
}
};

$form->addError(new FormError('Error 2!', null, array(), null, $cause));
$form->addError(new FormError('Error 3!', null, array(), null, 'code2'));
$formErrors = $form->getErrors();

$specificFormErrors = $formErrors->findByCodes($code);
$this->assertInstanceOf(FormErrorIterator::class, $specificFormErrors);
$this->assertCount($violationsCount, $specificFormErrors);
}

public function findByCodesProvider()
{
return array(
Expand Down