Skip to content

[DX][Form][Validator] Add ability check if cocrete constraint fails. #19496

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

Merged
merged 1 commit into from
Mar 22, 2017
Merged
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
22 changes: 22 additions & 0 deletions src/Symfony/Component/Form/FormErrorIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Form\Exception\InvalidArgumentException;
use Symfony\Component\Form\Exception\OutOfBoundsException;
use Symfony\Component\Form\Exception\BadMethodCallException;
use Symfony\Component\Validator\ConstraintViolation;

/**
* Iterates over the errors of a form.
Expand Down Expand Up @@ -265,6 +266,27 @@ public function seek($position)
}
}

/**
* Creates iterator for errors with specific codes.
*
* @param string|string[] $codes The codes to find
*
* @return static New instance which contains only specific errors.
*/
public function findByCodes($codes)
Copy link
Contributor

Choose a reason for hiding this comment

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

As this only works for errors caused by the validator component...im not sure coupling hardcoded is appropriate. It's convenient though.. but maybe this should be findByCause..

$errros->findByCause(function($cause) use ($codes) {
    return $cause instanceof ConstraintViolation && in_array($cause->getCode(), (array) $codes, true);
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I haven't see any benefits from public method findByCause - too much code for writing closure. It equals to writing foreach.

{
$codes = (array) $codes;
$errors = array();
foreach ($this as $error) {
$cause = $error->getCause();
if ($cause instanceof ConstraintViolation && in_array($cause->getCode(), $codes, true)) {
$errors[] = $error;
}
}

return new static($this->form, $errors);
}

/**
* Utility function for indenting multi-line strings.
*
Expand Down
62 changes: 62 additions & 0 deletions src/Symfony/Component/Form/Tests/FormErrorIteratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests;

use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormErrorIterator;
use Symfony\Component\Validator\ConstraintViolation;

class FormErrorIteratorTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider findByCodesProvider
*/
public function testFindByCodes($code, $violationsCount)
{
if (!class_exists(ConstraintViolation::class)) {
$this->markTestSkipped('Validator component required.');
}

$formBuilder = new FormBuilder(
'form',
null,
new EventDispatcher(),
$this->getMock('Symfony\Component\Form\FormFactoryInterface'),
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 ConstraintViolation('Error 2!', null, array(), null, '', null, null, 'code1');
$form->addError(new FormError('Error 2!', null, array(), null, $cause));
$cause = new ConstraintViolation('Error 3!', null, array(), null, '', null, null, 'code2');
$form->addError(new FormError('Error 3!', null, array(), null, $cause));
$formErrors = $form->getErrors();
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps should be tested with nested errors.


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

public function findByCodesProvider()
{
return array(
array('code1', 2),
array(array('code1', 'code2'), 3),
array('code3', 0),
);
}
}
20 changes: 20 additions & 0 deletions src/Symfony/Component/Validator/ConstraintViolationList.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,24 @@ public function offsetUnset($offset)
{
$this->remove($offset);
}

/**
* Creates iterator for errors with specific codes.
*
* @param string|string[] $codes The codes to find
*
* @return static New instance which contains only specific errors.
*/
public function findByCodes($codes)
{
$codes = (array) $codes;
$violations = array();
foreach ($this as $violation) {
if (in_array($violation->getCode(), $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.

$codes should be casted to array. Imo this may also be typehinted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ro0NL I've added support passing code or array of codes. No need wrap single code with array

Copy link
Contributor

Choose a reason for hiding this comment

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

Personally i'd favor API like

final public function findByCode($code)
{
   return $this->findByCodes(array($code));
}

public function findByCodes(array $codes)
{ }

But this is fine 👍

$violations[] = $violation;
}
}

return new static($violations);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,35 @@ public function testToString()
$this->assertEquals($expected, (string) $this->list);
}

protected function getViolation($message, $root = null, $propertyPath = null)
/**
* @dataProvider findByCodesProvider
*/
public function testFindByCodes($code, $violationsCount)
{
return new ConstraintViolation($message, $message, array(), $root, $propertyPath, null);
$violations = array(
$this->getViolation('Error', null, null, 'code1'),
$this->getViolation('Error', null, null, 'code1'),
$this->getViolation('Error', null, null, 'code2'),
);
$list = new ConstraintViolationList($violations);

$specificErrors = $list->findByCodes($code);

$this->assertInstanceOf(ConstraintViolationList::class, $specificErrors);
$this->assertCount($violationsCount, $specificErrors);
}

public function findByCodesProvider()
{
return array(
array('code1', 2),
array(array('code1', 'code2'), 3),
array('code3', 0),
);
}

protected function getViolation($message, $root = null, $propertyPath = null, $code = null)
{
return new ConstraintViolation($message, $message, array(), $root, $propertyPath, null, null, $code);
}
}