Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Form] Changed Form::getErrors() to return an iterator and added two optional parameters $deep and $flatten #9918

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 2 commits into from
Mar 11, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[Form] Changed Form::getErrors() to return an iterator and added two …
…optional parameters $deep and $flatten
  • Loading branch information
webmozart committed Jan 10, 2014
commit a9268c4a99e9be4c76d41f8f79c137c236ad8c13
39 changes: 39 additions & 0 deletions 39 UPGRADE-2.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,42 @@ Routing
-------

* Added a new optional parameter `$requiredSchemes` to `Symfony\Component\Routing\Generator\UrlGenerator::doGenerate()`

Form
----

* The method `FormInterface::getErrors()` now returns an instance of
`Symfony\Component\Form\FormErrorIterator` instead of an array. This object
is traversable, countable and supports array access. However, you can not
pass it to any of PHP's `array_*` functions anymore. You should use
`iterator_to_array()` in those cases where you did.

Before:

```
$errors = array_map($callback, $form->getErrors());
```

After:

```
$errors = array_map($callback, iterator_to_array($form->getErrors()));
```

* The method `FormInterface::getErrors()` now has two additional, optional
parameters. Make sure to add these parameters to the method signatures of
your implementations of that interface.

Before:

```
public function getErrors()
{
```

After:

```
public function getErrors($deep = false, $flatten = false)
{
```
16 changes: 16 additions & 0 deletions 16 UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,22 @@ UPGRADE FROM 2.x to 3.0
* The options "csrf_provider" and "intention" were renamed to "csrf_token_generator"
and "csrf_token_id".

* The method `Form::getErrorsAsString()` was removed. Use `Form::getErrors()`
instead with the argument `$deep` set to true and cast the returned iterator
to a string (if not done implicitly by PHP).

Before:

```
echo $form->getErrorsAsString();
```

After:

```
echo $form->getErrors(true);
```


### FrameworkBundle

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php if ($errors): ?>
<?php if (count($errors) > 0): ?>
<ul>
<?php foreach ($errors as $error): ?>
<li><?php echo $error->getMessage() ?></li>
Expand Down
6 changes: 4 additions & 2 deletions 6 src/Symfony/Component/Form/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,11 @@ public function all()
/**
* {@inheritdoc}
*/
public function getErrors()
public function getErrors($deep = false, $flatten = false)
{
return array();
$errors = array();

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

/**
Expand Down
3 changes: 3 additions & 0 deletions 3 src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ CHANGELOG
* added an option for multiple files upload
* form errors now reference their cause (constraint violation, exception, ...)
* form errors now remember which form they were originally added to
* [BC BREAK] added two optional parameters to FormInterface::getErrors() and
changed the method to return a Symfony\Component\Form\FormErrorIterator
instance instead of an array

2.4.0
-----
Expand Down
38 changes: 21 additions & 17 deletions 38 src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -778,9 +778,9 @@ public function getClickedButton()
/**
* {@inheritdoc}
*/
public function getErrors()
public function getErrors($deep = false, $flatten = false)
{
return $this->errors;
return new FormErrorIterator($this->errors, $this, $deep, $flatten);
}

/**
Expand All @@ -791,24 +791,13 @@ public function getErrors()
* @param integer $level The indentation level (used internally)
*
* @return string A string representation of all errors
*
* @deprecated Deprecated since version 2.5, to be removed in 3.0. Use
* {@link getErrors()} instead and cast the result to a string.
*/
public function getErrorsAsString($level = 0)
{
$errors = '';
foreach ($this->errors as $error) {
$errors .= str_repeat(' ', $level).'ERROR: '.$error->getMessage()."\n";
}

foreach ($this->children as $key => $child) {
$errors .= str_repeat(' ', $level).$key.":\n";
if ($child instanceof self && $err = $child->getErrorsAsString($level + 4)) {
$errors .= $err;
} else {
$errors .= str_repeat(' ', $level + 4)."No errors\n";
}
}

return $errors;
return self::indent((string) $this->getErrors(true), $level);
}

/**
Expand Down Expand Up @@ -1115,4 +1104,19 @@ private function viewToNorm($value)

return $value;
}

/**
* Utility function for indenting multi-line strings.
*
* @param string $string The string
* @param integer $level The number of spaces to use for indentation
*
* @return string The indented string
*/
private static function indent($string, $level)
{
$indentation = str_repeat(' ', $level);

return rtrim($indentation.str_replace("\n", "\n".$indentation, $string), ' ');
}
}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.