feature #10418 [Form] Removed "magic" from FormErrorIterator (webmozart)
This PR was merged into the 2.5-dev branch.
Discussion
----------
[Form] Removed "magic" from FormErrorIterator
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
Commits
-------
daac66e
[Form] Removed "magic" from FormErrorIterator
This commit is contained in:
commit
c2d4be1d6b
@ -186,9 +186,7 @@ class Button implements \IteratorAggregate, FormInterface
|
|||||||
*/
|
*/
|
||||||
public function getErrors($deep = false, $flatten = true)
|
public function getErrors($deep = false, $flatten = true)
|
||||||
{
|
{
|
||||||
$errors = array();
|
return new FormErrorIterator($this, array());
|
||||||
|
|
||||||
return new FormErrorIterator($errors, $this, $deep, $flatten);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -780,7 +780,33 @@ class Form implements \IteratorAggregate, FormInterface
|
|||||||
*/
|
*/
|
||||||
public function getErrors($deep = false, $flatten = true)
|
public function getErrors($deep = false, $flatten = true)
|
||||||
{
|
{
|
||||||
return new FormErrorIterator($this->errors, $this, $deep, $flatten);
|
$errors = $this->errors;
|
||||||
|
|
||||||
|
// Copy the errors of nested forms to the $errors array
|
||||||
|
if ($deep) {
|
||||||
|
foreach ($this as $child) {
|
||||||
|
/** @var FormInterface $child */
|
||||||
|
if ($child->isSubmitted() && $child->isValid()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$iterator = $child->getErrors(true, $flatten);
|
||||||
|
|
||||||
|
if (0 === count($iterator)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($flatten) {
|
||||||
|
foreach ($iterator as $error) {
|
||||||
|
$errors[] = $error;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$errors[] = $iterator;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new FormErrorIterator($this, $errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Form;
|
namespace Symfony\Component\Form;
|
||||||
|
|
||||||
|
use Symfony\Component\Form\Exception\InvalidArgumentException;
|
||||||
use Symfony\Component\Form\Exception\OutOfBoundsException;
|
use Symfony\Component\Form\Exception\OutOfBoundsException;
|
||||||
use Symfony\Component\Form\Exception\BadMethodCallException;
|
use Symfony\Component\Form\Exception\BadMethodCallException;
|
||||||
|
|
||||||
@ -44,38 +45,33 @@ class FormErrorIterator implements \RecursiveIterator, \SeekableIterator, \Array
|
|||||||
private $form;
|
private $form;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Boolean
|
* @var FormError[]|FormErrorIterator[]
|
||||||
*/
|
*/
|
||||||
private $deep;
|
private $errors;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var Boolean
|
|
||||||
*/
|
|
||||||
private $flatten;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private $elements;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new iterator.
|
* Creates a new iterator.
|
||||||
*
|
*
|
||||||
* @param array $errors The iterated errors
|
* @param FormInterface $form The erroneous form
|
||||||
* @param FormInterface $form The form the errors belong to
|
* @param array $errors The form errors
|
||||||
* @param Boolean $deep Whether to include the errors of child
|
*
|
||||||
* forms
|
* @throws InvalidArgumentException If the errors are invalid
|
||||||
* @param Boolean $flatten Whether to flatten the recursive list of
|
|
||||||
* errors into a flat list
|
|
||||||
*/
|
*/
|
||||||
public function __construct(array &$errors, FormInterface $form, $deep = false, $flatten = true)
|
public function __construct(FormInterface $form, array $errors)
|
||||||
{
|
{
|
||||||
$this->errors = &$errors;
|
foreach ($errors as $error) {
|
||||||
$this->form = $form;
|
if (!($error instanceof FormError || $error instanceof self)) {
|
||||||
$this->deep = $deep;
|
throw new InvalidArgumentException(sprintf(
|
||||||
$this->flatten = $flatten;
|
'The errors must be instances of '.
|
||||||
|
'"\Symfony\Component\Form\FormError" or "%s". Got: "%s".',
|
||||||
|
__CLASS__,
|
||||||
|
is_object($error) ? get_class($error) : gettype($error)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$this->rewind();
|
$this->form = $form;
|
||||||
|
$this->errors = $errors;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -87,13 +83,13 @@ class FormErrorIterator implements \RecursiveIterator, \SeekableIterator, \Array
|
|||||||
{
|
{
|
||||||
$string = '';
|
$string = '';
|
||||||
|
|
||||||
foreach ($this->elements as $element) {
|
foreach ($this->errors as $error) {
|
||||||
if ($element instanceof FormError) {
|
if ($error instanceof FormError) {
|
||||||
$string .= 'ERROR: '.$element->getMessage()."\n";
|
$string .= 'ERROR: '.$error->getMessage()."\n";
|
||||||
} else {
|
} else {
|
||||||
/** @var $element FormErrorIterator */
|
/** @var $error FormErrorIterator */
|
||||||
$string .= $element->form->getName().":\n";
|
$string .= $error->form->getName().":\n";
|
||||||
$string .= self::indent((string) $element);
|
$string .= self::indent((string) $error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,12 +109,12 @@ class FormErrorIterator implements \RecursiveIterator, \SeekableIterator, \Array
|
|||||||
/**
|
/**
|
||||||
* Returns the current element of the iterator.
|
* Returns the current element of the iterator.
|
||||||
*
|
*
|
||||||
* @return FormError|FormErrorIterator An error or an iterator for nested
|
* @return FormError|FormErrorIterator An error or an iterator containing
|
||||||
* errors.
|
* nested errors.
|
||||||
*/
|
*/
|
||||||
public function current()
|
public function current()
|
||||||
{
|
{
|
||||||
return current($this->elements);
|
return current($this->errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -126,7 +122,7 @@ class FormErrorIterator implements \RecursiveIterator, \SeekableIterator, \Array
|
|||||||
*/
|
*/
|
||||||
public function next()
|
public function next()
|
||||||
{
|
{
|
||||||
next($this->elements);
|
next($this->errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -136,7 +132,7 @@ class FormErrorIterator implements \RecursiveIterator, \SeekableIterator, \Array
|
|||||||
*/
|
*/
|
||||||
public function key()
|
public function key()
|
||||||
{
|
{
|
||||||
return key($this->elements);
|
return key($this->errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -146,7 +142,7 @@ class FormErrorIterator implements \RecursiveIterator, \SeekableIterator, \Array
|
|||||||
*/
|
*/
|
||||||
public function valid()
|
public function valid()
|
||||||
{
|
{
|
||||||
return null !== key($this->elements);
|
return null !== key($this->errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -157,32 +153,7 @@ class FormErrorIterator implements \RecursiveIterator, \SeekableIterator, \Array
|
|||||||
*/
|
*/
|
||||||
public function rewind()
|
public function rewind()
|
||||||
{
|
{
|
||||||
$this->elements = $this->errors;
|
reset($this->errors);
|
||||||
|
|
||||||
if ($this->deep) {
|
|
||||||
foreach ($this->form as $child) {
|
|
||||||
/** @var FormInterface $child */
|
|
||||||
if ($child->isSubmitted() && $child->isValid()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$iterator = $child->getErrors(true, $this->flatten);
|
|
||||||
|
|
||||||
if (0 === count($iterator)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->flatten) {
|
|
||||||
foreach ($iterator as $error) {
|
|
||||||
$this->elements[] = $error;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$this->elements[] = $iterator;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
reset($this->elements);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -194,7 +165,7 @@ class FormErrorIterator implements \RecursiveIterator, \SeekableIterator, \Array
|
|||||||
*/
|
*/
|
||||||
public function offsetExists($position)
|
public function offsetExists($position)
|
||||||
{
|
{
|
||||||
return isset($this->elements[$position]);
|
return isset($this->errors[$position]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -208,11 +179,11 @@ class FormErrorIterator implements \RecursiveIterator, \SeekableIterator, \Array
|
|||||||
*/
|
*/
|
||||||
public function offsetGet($position)
|
public function offsetGet($position)
|
||||||
{
|
{
|
||||||
if (!isset($this->elements[$position])) {
|
if (!isset($this->errors[$position])) {
|
||||||
throw new OutOfBoundsException('The offset '.$position.' does not exist.');
|
throw new OutOfBoundsException('The offset '.$position.' does not exist.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->elements[$position];
|
return $this->errors[$position];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -243,7 +214,7 @@ class FormErrorIterator implements \RecursiveIterator, \SeekableIterator, \Array
|
|||||||
*/
|
*/
|
||||||
public function hasChildren()
|
public function hasChildren()
|
||||||
{
|
{
|
||||||
return current($this->elements) instanceof self;
|
return current($this->errors) instanceof self;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -251,7 +222,7 @@ class FormErrorIterator implements \RecursiveIterator, \SeekableIterator, \Array
|
|||||||
*/
|
*/
|
||||||
public function getChildren()
|
public function getChildren()
|
||||||
{
|
{
|
||||||
return current($this->elements);
|
return current($this->errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -273,7 +244,7 @@ class FormErrorIterator implements \RecursiveIterator, \SeekableIterator, \Array
|
|||||||
*/
|
*/
|
||||||
public function count()
|
public function count()
|
||||||
{
|
{
|
||||||
return count($this->elements);
|
return count($this->errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -285,14 +256,14 @@ class FormErrorIterator implements \RecursiveIterator, \SeekableIterator, \Array
|
|||||||
*/
|
*/
|
||||||
public function seek($position)
|
public function seek($position)
|
||||||
{
|
{
|
||||||
if (!isset($this->elements[$position])) {
|
if (!isset($this->errors[$position])) {
|
||||||
throw new OutOfBoundsException('The offset '.$position.' does not exist.');
|
throw new OutOfBoundsException('The offset '.$position.' does not exist.');
|
||||||
}
|
}
|
||||||
|
|
||||||
reset($this->elements);
|
reset($this->errors);
|
||||||
|
|
||||||
while ($position !== key($this->elements)) {
|
while ($position !== key($this->errors)) {
|
||||||
next($this->elements);
|
next($this->errors);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user