Skip to content

Instantly share code, notes, and snippets.

@sylvaincombes
Created April 29, 2016 12:59
Show Gist options
  • Save sylvaincombes/66b480809d501d6b0c55ab75ecd869d4 to your computer and use it in GitHub Desktop.
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)
<?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