-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Created FormErrorBag
This is fully BC with returning an array and it deprecates Form::getErrorsAsString()
- Loading branch information
commit 725d9eefd0e60c53426234641ebb60f1b2ff6551
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,233 @@ | ||
<?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) | ||
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 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() | ||
{ | ||
return false === 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() | ||
{ | ||
$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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.