Created
March 27, 2013 22:03
-
-
Save jmolivas/5258493 to your computer and use it in GitHub Desktop.
get error messages from Form element symfony 2.0
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 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