-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Created FormErrorBag (Approach 2) #9914
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
<?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; | ||
|
||
use Symfony\Component\Form\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* A bag of Form Errors. | ||
* | ||
* @author Wouter J <wouter@wouterj.nl> | ||
* | ||
* @since v2.5 | ||
*/ | ||
class FormErrorBag implements \Countable, \ArrayAccess, \IteratorAggregate | ||
{ | ||
/** | ||
* @var array An array of FormError and FormErrorBag instances | ||
*/ | ||
protected $errors = array(); | ||
|
||
private $formName; | ||
|
||
public function setFormName($name) | ||
{ | ||
$this->formName = $name; | ||
} | ||
|
||
/** | ||
* Adds a new form error. | ||
* | ||
* @param FormError $error | ||
*/ | ||
public function addError(FormError $error) | ||
{ | ||
$this->errors[] = $error; | ||
} | ||
|
||
/** | ||
* Adds a new form error collection. | ||
* | ||
* @param string $formName | ||
* @param FormErrorBag $collection | ||
*/ | ||
public function addCollection($formName, $collection) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the collection should be typehinted |
||
{ | ||
$collection->setFormName($formName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. modifying the collection passed as argument would lead to weird side effects |
||
|
||
$this->errors[$formName] = $collection; | ||
} | ||
|
||
public function count() | ||
{ | ||
$count = 0; | ||
|
||
foreach ($this->errors as $error) { | ||
if ($error instanceof FormError) { | ||
$count++; | ||
} | ||
} | ||
|
||
return $count; | ||
} | ||
|
||
/** | ||
* Counts all errors, including errors from children. | ||
* | ||
* @return int | ||
*/ | ||
public function countAll() | ||
{ | ||
$count = 0; | ||
|
||
foreach ($this->errors as $error) { | ||
if ($error instanceof FormErrorBag) { | ||
$count += $error->countAll(); | ||
} else { | ||
$count++; | ||
} | ||
} | ||
|
||
return $count; | ||
} | ||
|
||
public function get($offset) | ||
{ | ||
return $this->errors[$offset]; | ||
} | ||
|
||
public function set($offset, $value) | ||
{ | ||
$this->errors[$offset] = $value; | ||
} | ||
|
||
public function has($offset) | ||
{ | ||
return isset($this->errors[$offset]); | ||
} | ||
|
||
public function all() | ||
{ | ||
return $this->errors; | ||
} | ||
|
||
public function clear() | ||
{ | ||
$this->replace(); | ||
} | ||
|
||
public function remove($offset) | ||
{ | ||
unset($this->errors[$offset]); | ||
} | ||
|
||
public function replace(array $errors = array()) | ||
{ | ||
$this->errors = $errors; | ||
} | ||
|
||
public function isEmpty() | ||
{ | ||
return empty($this->errors); | ||
} | ||
|
||
public function keys() | ||
{ | ||
return array_keys($this->errors); | ||
} | ||
|
||
public function __toString() | ||
{ | ||
$level = func_num_args() > 0 ? func_get_arg(0) : 0; | ||
$errors = ''; | ||
|
||
foreach ($this->errors as $key => $error) { | ||
if ($error instanceof self) { | ||
$errors .= str_repeat(' ', $level).$key.":\n"; | ||
if ($err = $error->__toString($level + 4)) { | ||
$errors .= $err; | ||
} else { | ||
$errors .= str_repeat(' ', $level + 4)."No errors\n"; | ||
} | ||
} else { | ||
$errors .= str_repeat(' ', $level).'ERROR: '.$error->getMessage()."\n"; | ||
} | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
public function offsetExists($offset) | ||
{ | ||
return $this->has($offset) && $this->errors[$offset] instanceof FormError; | ||
} | ||
|
||
public function offsetGet($offset) | ||
{ | ||
$error = $this->get($offset); | ||
|
||
if ($error instanceof FormError) { | ||
return $error; | ||
} | ||
} | ||
|
||
public function offsetSet($offset, $value) | ||
{ | ||
return $this->set($offset); | ||
} | ||
|
||
public function offsetUnset($offset) | ||
{ | ||
return $this->remove($offset); | ||
} | ||
|
||
public function getIterator() | ||
{ | ||
$errors = array_filter($this->errors, function ($error) { | ||
return $error instanceof FormError; | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should use a FilterIterator to filter this lazily |
||
|
||
return new \ArrayIterator($errors); | ||
} | ||
|
||
public function getAllErrors() | ||
{ | ||
return new \RecursiveArrayIterator($this->errors); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does it work well for deep errors ? I think RecursiveArrayIterator will use the ArrayIterator on the child errors, not the recursive errors |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't use the leading undescore in Symfony