-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Created FormErrorBag #9099
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,241 @@ | ||
<?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 \RecursiveIterator, \Countable, \ArrayAccess | ||
{ | ||
/** | ||
* @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) | ||
{ | ||
$collection->setFormName($formName); | ||
|
||
$this->errors[$formName] = $collection; | ||
} | ||
|
||
public function current() | ||
{ | ||
$current = current($this->errors); | ||
|
||
if (!$current instanceof FormError) { | ||
$this->next(); | ||
|
||
if ($this->valid()) { | ||
$current = $this->current(); | ||
} | ||
} | ||
|
||
return $current; | ||
} | ||
|
||
public function key() | ||
{ | ||
return isset($this->formName) ? $this->formName : key($this->errors); | ||
} | ||
|
||
public function next() | ||
{ | ||
return next($this->errors); | ||
} | ||
|
||
public function rewind() | ||
{ | ||
reset($this->errors); | ||
} | ||
|
||
public function valid() | ||
{ | ||
while (current($this->errors) instanceof FormErrorBag) { | ||
$this->next(); | ||
|
||
if (!$this->valid()) { | ||
return false; | ||
} | ||
} | ||
|
||
return null !== key($this->errors); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function hasChildren() | ||
{ | ||
return current($this->errors) instanceof FormErrorBag; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getChildren() | ||
{ | ||
return current($this->errors); | ||
} | ||
|
||
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() | ||
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. missing phpdoc for many methods 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. do we really want to have PHPdoc for PHP build-in methods, like __toString etc? 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. no 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. yes because otherwise it's not documented what the string representation actually returns 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. and this is one of the main points of FormErrorBag 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. also its not doccumented that it accepts a parameter 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 string representation wasn't documented on getErrorsAsString() too, I don't really know why we should do it here. And the parameter is only for internal use. 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. where is the parameter used internally? 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. It's used to indent the children forms with 4 spaces 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. ok as long as its not used outside the class |
||
{ | ||
$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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,7 +94,7 @@ public function all(); | |
/** | ||
* Returns all errors. | ||
* | ||
* @return FormError[] An array of FormError instances that occurred during validation | ||
* @return FormErrorBag | ||
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. shouldn't there be an interface? since there is an interface for everything else 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. Ping @bschussek 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. hm there was no interface for FormError before as well. so it might no be needed because it would complicate things |
||
*/ | ||
public function getErrors(); | ||
|
||
|
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.
FormErrorBag|array