Last active
October 9, 2017 09:04
-
-
Save dework/4968094 to your computer and use it in GitHub Desktop.
Symfony 2 Get All Form Errors
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 Chyrius\SiteBundle\Form; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Symfony\Component\Form\Form; | |
/** | |
* @todo Обрабатывать так же ошибки детей-детей | |
*/ | |
class FormErrors | |
{ | |
private $container; | |
/** | |
* @param \Symfony\Component\Form\Form $form | |
*/ | |
public function __construct(ContainerInterface $container) | |
{ | |
$this->container = $container; | |
} | |
/** | |
* @param \Symfony\Component\Form\Form $form | |
* @return array | |
*/ | |
public function getFormErrors(Form $form) | |
{ | |
// | |
if ($err = $this->childErrors($form)) { | |
$errors["form"] = $err; | |
} | |
// | |
foreach ($form->all() as $key => $child) { | |
// | |
if ($err = $this->childErrors($child)) { | |
$errors[$key] = $err; | |
} | |
} | |
return $errors; | |
} | |
/** | |
* @param \Symfony\Component\Form\Form $form | |
* @return array | |
*/ | |
public function childErrors(Form $form) | |
{ | |
$errors = array(); | |
foreach ($form->getErrors() as $error) { | |
// Переведем ошибку, если есть перевод | |
$message = $this->container->get('translator')->trans($error->getMessage(), array(), 'validators'); | |
array_push($errors, $message); | |
} | |
return $errors; | |
} | |
} |
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
services: | |
chyrius.form_errors: | |
class: Chyrius\SiteBundle\Form\FormErrors | |
arguments: [ @service_container ] |
Я бы немного ещё добавил
foreach ($form->all() as $key => $child) {
if($child instanceof \Symfony\Component\Form\Form){
if ($err = $this->childErrors($child)) {
$errors[$key] = $err;
}
}
}
Т.к. иногда SubmitButton тоже уже есть в форме
Что в принципе решено во 2 варианте ))
Спасибо!
Korpch's function seems to be broken. Did not work as expected for me.
yapro's version worked, but was missing translation. Also it generated an array of arrays, which is not what i expected to get. I was looking for a way to get a simple plain list of error messages to display them to the user.
My final version:
/**
* @param \Symfony\Component\Form\Form $form
* @return string[]
*/
public static function getFormErrors(\Symfony\Component\Form\Form $form)
{
$errors = array();
if ($form instanceof \Symfony\Component\Form\Form) {
// find errors of this element
foreach ($form->getErrors() as $error) {
$errors[] = self::$translator->trans($error->getMessage(), array(), 'validators');
}
// iterate over errors of all children
foreach ($form->all() as $key => $child) {
if($child instanceof \Symfony\Component\Form\Form) {
/** @var $child \Symfony\Component\Form\Form */
$err = self::getFormErrors($child);
if (count($err) > 0) {
$errors = array_merge($errors, $err);
}
}
}
}
return $errors;
}
@yapro о привет
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank You!