Add ability retrieve errors by their code.
This commit is contained in:
parent
983b560e15
commit
29a3a7e0d6
@ -14,6 +14,7 @@ namespace Symfony\Component\Form;
|
||||
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.
|
||||
@ -265,6 +266,27 @@ class FormErrorIterator implements \RecursiveIterator, \SeekableIterator, \Array
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
$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.
|
||||
*
|
||||
|
62
src/Symfony/Component/Form/Tests/FormErrorIteratorTest.php
Normal file
62
src/Symfony/Component/Form/Tests/FormErrorIteratorTest.php
Normal file
@ -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();
|
||||
|
||||
$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),
|
||||
);
|
||||
}
|
||||
}
|
@ -158,4 +158,24 @@ class ConstraintViolationList implements \IteratorAggregate, ConstraintViolation
|
||||
{
|
||||
$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)) {
|
||||
$violations[] = $violation;
|
||||
}
|
||||
}
|
||||
|
||||
return new static($violations);
|
||||
}
|
||||
}
|
||||
|
@ -127,8 +127,35 @@ EOF;
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user