8000 [Form] Removed "magic" from FormErrorIterator by webmozart · Pull Request #10418 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Removed "magic" from FormErrorIterator #10418

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 11, 2014
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
4 changes: 1 addition & 3 deletions src/Symfony/Component/Form/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,7 @@ public function all()
*/
public function getErrors($deep = false, $flatten = true)
{
$errors = array();

return new FormErrorIterator($errors, $this, $deep, $flatten);
return new FormErrorIterator($this, array());
}

/**
Expand Down
28 changes: 27 additions & 1 deletion src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,33 @@ public function getClickedButton()
*/
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);
}

/**
Expand Down
115 changes: 43 additions & 72 deletions src/Symfony/Component/Form/FormErrorIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Form;

use Symfony\Component\Form\Exception\InvalidArgumentException;
use Symfony\Component\Form\Exception\OutOfBoundsException;
use Symfony\Component\Form\Exception\BadMethodCallException;

Expand Down Expand Up @@ -44,38 +45,33 @@ class FormErrorIterator implements \RecursiveIterator, \SeekableIterator, \Array
private $form;

/**
* @var Boolean
* @var FormError[]|FormErrorIterator[]
Copy link
Contributor

Choose a reason for hiding this comment

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

should be (FormError|FormErrorIterator)[] because it can be mixed
http://www.phpdoc.org/docs/latest/references/phpdoc/types.html#abnf

*/
private $deep;

/**
* @var Boolean
*/
private $flatten;

/**
* @var array
*/
private $elements;
private $errors;

/**
* Creates a new iterator.
*
* @param array $errors The iterated errors
* @param FormInterface $form The form the errors belong to
* @param Boolean $deep Whether to include the errors of child
* forms
* @param Boolean $flatten Whether to flatten the recursive list of
* errors into a flat list
* @param FormInterface $form The erroneous form
* @param array $errors The form errors
*
* @throws InvalidArgumentException If the errors are invalid
*/
public function __construct(array &$errors, FormInterface $form, $deep = false, $flatten = true)
public function __construct(FormInterface $form, array $errors)
{
$this->errors = &$errors;
$this->form = $form;
$this->deep = $deep;
$this->flatten = $flatten;
foreach ($errors as $error) {
if (!($error instanceof FormError || $error instanceof self)) {
throw new InvalidArgumentException(sprintf(
'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;
}

/**
Expand All @@ -87,13 +83,13 @@ public function __toString()
{
$string = '';

foreach ($this->elements as $element) {
if ($element instanceof FormError) {
$string .= 'ERROR: '.$element->getMessage()."\n";
foreach ($this->errors as $error) {
if ($error instanceof FormError) {
$string .= 'ERROR: '.$error->getMessage()."\n";
} else {
/** @var $element FormErrorIterator */
$string .= $element->form->getName().":\n";
$string .= self::indent((string) $element);
/** @var $error FormErrorIterator */
$string .= $error->form->getName().":\n";
$string .= self::indent((string) $error);
}
}

Expand All @@ -113,20 +109,20 @@ public function getForm()
/**
* Returns the current element of the iterator.
*
* @return FormError|FormErrorIterator An error or an iterator for nested
* errors.
* @return FormError|FormErrorIterator An error or an iterator containing
* nested errors.
*/
public function current()
{
return current($this->elements);
return current($this->errors);
}

/**
* Advances the iterator to the next position.
*/
public function next()
{
next($this->elements);
next($this->errors);
}

/**
Expand All @@ -136,7 +132,7 @@ public function next()
*/
public function key()
{
return key($this->elements);
return key($this->errors);
}

/**
Expand All @@ -146,7 +142,7 @@ public function key()
*/
public function valid()
{
return null !== key($this->elements);
return null !== key($this->errors);
}

/**
Expand All @@ -157,32 +153,7 @@ public function valid()
*/
public function rewind()
{
$this->elements = $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);
reset($this->errors);
}

/**
Expand All @@ -194,7 +165,7 @@ public function rewind()
*/
public function offsetExists($position)
{
return isset($this->elements[$position]);
return isset($this->errors[$position]);
}

/**
Expand All @@ -208,11 +179,11 @@ public function offsetExists($position)
*/
public function offsetGet($position)
{
if (!isset($this->elements[$position])) {
if (!isset($this->errors[$position])) {
throw new OutOfBoundsException('The offset '.$position.' does not exist.');
}

return $this->elements[$position];
return $this->errors[$position];
}

/**
Expand Down Expand Up @@ -243,15 +214,15 @@ public function offsetUnset($position)
*/
public function hasChildren()
{
return current($this->elements) instanceof self;
return current($this->errors) instanceof self;
}

/**
* Alias of {@link current()}.
*/
public function getChildren()
{
return current($this->elements);
return current($this->errors);
}

/**
Expand All @@ -273,7 +244,7 @@ public function getChildren()
*/
public function count()
{
return count($this->elements);
return count($this->errors);
}

/**
Expand All @@ -285,14 +256,14 @@ public function count()
*/
public function seek($position)
{
if (!isset($this->elements[$position])) {
if (!isset($this->errors[$position])) {
throw new OutOfBoundsException('The offset '.$position.' does not exist.');
}

reset($this->elements);
reset($this->errors);

while ($position !== key($this->elements)) {
next($this->elements);
while ($position !== key($this->errors)) {
next($this->errors);
}
}

Expand Down
0