Skip to content

Instantly share code, notes, and snippets.

@czettnersandor
Forked from umpirsky/getErrorMessages.php
Last active December 10, 2015 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save czettnersandor/4442335 to your computer and use it in GitHub Desktop.
Save czettnersandor/4442335 to your computer and use it in GitHub Desktop.
Symfony 2: Retrieves every error message for the form to a simple array. Including the field error messages.
<?php
private function getErrorMessages(\Symfony\Component\Form\Form $form) {
$errors = array();
foreach ($form->getErrors() as $key => $error) {
$errors[] = $error->getMessage();
}
if ($form->hasChildren()) {
foreach ($form->getChildren() as $child) {
if (!$child->isValid()) {
$errors = array_merge($errors, $this->getErrorMessages($child));
}
}
}
return $errors;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment