Skip to content

Instantly share code, notes, and snippets.

@tystr
Created January 18, 2013 05:33
Show Gist options
  • Save tystr/4562577 to your computer and use it in GitHub Desktop.
Save tystr/4562577 to your computer and use it in GitHub Desktop.
Gets symfony2 form errors as an array.
<?php
protected function getErrorsForForm(Form $form)
{
$errors = [];
foreach ($form->all() as $child) {
foreach ($child->getErrors() as $error) {
$errors[$child->getName()][] = $error->getMessage();
}
if ($child->count() > 0) {
$childErrors = $this->getErrorsForForm($child);
if (!empty($childErrors)) {
$errors[$child->getName()] = $childErrors;
}
}
}
return $errors;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment