Created
April 29, 2016 12:59
-
-
Save sylvaincombes/66b480809d501d6b0c55ab75ecd869d4 to your computer and use it in GitHub Desktop.
Symfony get all errors on form with collections (children with form validate and errors)
This file contains 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
<?php | |
namespace AppBundle\Utils; | |
use Symfony\Component\Form\Form; | |
/** | |
* Class FormHelper | |
* | |
* @package AppBundle\Utils | |
*/ | |
class FormHelper | |
{ | |
/** | |
* Recursive helper who return all found errors in a form | |
* | |
* @param Form $form | |
* | |
* @return array | |
*/ | |
public static function getFormErrors(Form $form) | |
{ | |
$errors = []; | |
// find errors of this element | |
foreach ($form->getErrors() as $error) { | |
$errors[] = $error; | |
} | |
// iterate over errors of all children | |
foreach ($form->all() as $key => $child) { | |
if ($child instanceof Form) { | |
/** | |
* @var Form $child | |
*/ | |
$err = self::getFormErrors($child); | |
if (count($err) > 0) { | |
$errors = array_merge($errors, $err); | |
} | |
} | |
} | |
return $errors; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment