Skip to content

Instantly share code, notes, and snippets.

@jmolivas
Created March 27, 2013 22:03
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 jmolivas/5258493 to your computer and use it in GitHub Desktop.
Save jmolivas/5258493 to your computer and use it in GitHub Desktop.
get error messages from Form element symfony 2.0
<?php
namespace Company\YourBundle\Util;
/**
* FormUtil
*
* This class was created to add Form resusable functions
*
*/
class FormUtil
{
/**
* Extract error messages from Form.
* @param Form $form
*
* @return array
*
*/
public static function getErrorMessages(\Symfony\Component\Form\Form $form)
{
$errors = array();
foreach ($form->getErrors() as $key => $error) {
$template = $error->getMessageTemplate();
$parameters = $error->getMessageParameters();
foreach ($parameters as $var => $value) {
$template = str_replace($var, $value, $template);
}
$errors[$key] = $template;
}
if ($form->hasChildren()) {
foreach ($form->getChildren() as $child) {
if (!$child->isValid()) {
$errors[$child->getName()] = self::getErrorMessages($child);
}
}
}
return $errors;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment