Symfony2: jak uzyskać błędy walidacji formularza po powiązaniu żądania z formularzem

Oto Mój saveAction kod (do którego formularz przekazuje dane)

public function saveAction()
{
    $user = OBUser();

    $form = $this->createForm(new OBUserType(), $user);

    if ($this->request->getMethod() == 'POST')
    {
        $form->bindRequest($this->request);
        if ($form->isValid())
            return $this->redirect($this->generateUrl('success_page'));
        else
            return $this->redirect($this->generateUrl('registration_form'));
    } else
        return new Response();
}

Moje pytanie brzmi: jak uzyskać błędy, Jeśli $form->isValid() zwraca false?

 104
Author: Trix, 2011-08-08

19 answers

Możesz to zrobić na dwa sposoby:

  • nie przekierowuj użytkownika na błąd i wyświetl {{ form_errors(form) }} w pliku szablonu
  • access error array as $form->getErrors()
 110
Author: nefo_x,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-24 23:40:29

Symfony 2.3 / 2.4:

Ta funkcja pobiera wszystkie błędy. Te na formularzu jak "token CSRF jest nieprawidłowy. Proszę spróbować ponownie przesłać formularz.", a także dodatkowe błędy na formularzu, które nie mają błędu.

private function getErrorMessages(\Symfony\Component\Form\Form $form) {
    $errors = array();

    foreach ($form->getErrors() as $key => $error) {
        if ($form->isRoot()) {
            $errors['#'][] = $error->getMessage();
        } else {
            $errors[] = $error->getMessage();
        }
    }

    foreach ($form->all() as $child) {
        if (!$child->isValid()) {
            $errors[$child->getName()] = $this->getErrorMessages($child);
        }
    }

    return $errors;
}

Aby uzyskać wszystkie błędy jako ciąg znaków:

$string = var_export($this->getErrorMessages($form), true);

Symfony 2.5 / 3.0:

$string = (string) $form->getErrors(true, false);

Docs:
https://github.com/symfony/symfony/blob/master/UPGRADE-2.5.md#form https://github.com/symfony/symfony/blob/master/UPGRADE-3.0.md#form (na dole: The method Form::getErrorsAsString() was removed)

 93
Author: Flip,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-15 21:02:27

Poniżej znajduje się rozwiązanie, które zadziałało dla mnie. Ta funkcja znajduje się w kontrolerze i zwróci uporządkowaną tablicę wszystkich komunikatów o błędach oraz pola, które je spowodowały.

Symfony 2.0:

private 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()] = $this->getErrorMessages($child);
            }
        }
    }

    return $errors;
}

Symfony 2.1 i nowsze:

private function getErrorMessages(\Symfony\Component\Form\Form $form) {      
    $errors = array();

    if ($form->hasChildren()) {
        foreach ($form->getChildren() as $child) {
            if (!$child->isValid()) {
                $errors[$child->getName()] = $this->getErrorMessages($child);
            }
        }
    } else {
        foreach ($form->getErrors() as $key => $error) {
            $errors[] = $error->getMessage();
        }   
    }

    return $errors;
}
 47
Author: Icode4food,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-26 13:37:05

Użyj walidatora, aby uzyskać błędy dla określonego podmiotu

if( $form->isValid() )
{
    // ...
}
else
{
    // get a ConstraintViolationList
    $errors = $this->get('validator')->validate( $user );

    $result = '';

    // iterate on it
    foreach( $errors as $error )
    {
        // Do stuff with:
        //   $error->getPropertyPath() : the field that caused the error
        //   $error->getMessage() : the error message
    }
}

Odniesienie do API:

 33
Author: Olivier 'Ölbaum' Scherler,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-12 11:42:19

Aby uzyskać poprawne (tłumaczalne) wiadomości, obecnie używające SF 2.6.3, oto moja ostateczna funkcja (ponieważ żadna z powyższych nie działa już):

 private function getErrorMessages(\Symfony\Component\Form\Form $form) {      
    $errors = array();
    foreach ($form->getErrors(true, false) as $error) {
        // My personnal need was to get translatable messages
        // $errors[] = $this->trans($error->current()->getMessage());
        $errors[] = $error->current()->getMessage();
    }

    return $errors;
}

Jako metoda Form::getErrors() zwraca teraz instancję FormErrorIterator, chyba że zmienisz drugi argument ($flatten) na true. (Zwróci następnie instancję FormError i będziesz musiał wywołać metodę getMessage() bezpośrednio, bez metody current ():

 private function getErrorMessages(\Symfony\Component\Form\Form $form) {      
    $errors = array();
    foreach ($form->getErrors(true, true) as $error) {
        // My personnal need was to get translatable messages
        // $errors[] = $this->trans($error->getMessage());
        $errors[] = $error->getMessage();
    }

    return $errors;
}

)

Najbardziej ważne jest, aby ustawić pierwszy argument na true, aby uzyskać błędy. Pozostawienie drugiego argumentu ($flatten) do jego domyślnej wartości (true) zwróci instancje FormError, natomiast zwróci instancje FormErrorIterator, gdy zostanie ustawione na false.

 19
Author: Cedo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-04 14:19:01

Funkcja dla symfony 2.1 i nowszych, bez żadnej przestarzałej funkcji:

/**
 * @param \Symfony\Component\Form\Form $form
 *
 * @return array
 */
private function getErrorMessages(\Symfony\Component\Form\Form $form)
{
    $errors = array();

    if ($form->count() > 0) {
        foreach ($form->all() as $child) {
            /**
             * @var \Symfony\Component\Form\Form $child
             */
            if (!$child->isValid()) {
                $errors[$child->getName()] = $this->getErrorMessages($child);
            }
        }
    } else {
        /**
         * @var \Symfony\Component\Form\FormError $error
         */
        foreach ($form->getErrors() as $key => $error) {
            $errors[] = $error->getMessage();
        }
    }

    return $errors;
}
 15
Author: stwe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-28 22:49:22

Dla moich wiadomości flash byłem zadowolony z $form->getErrorsAsString()

Edit (from Benji_X80): Do stosowania SF3 $form->getErrors(true, false);

 15
Author: Tjorriemorrie,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-13 07:59:02

Przetłumaczone Komunikaty Błędów Formularza (Symfony2.1)

Bardzo się starałem znaleźć te informacje, więc myślę, że zdecydowanie warto dodać notkę o tłumaczeniu błędów formularzy.

@Icode4food ODPOWIEDŹ zwróci wszystkie błędy formularza. Zwracana tablica nie bierze jednak pod uwagę mnogości wiadomości ani tłumaczenia .

Możesz zmodyfikować pętlę foreach odpowiedzi @Icode4food, aby mieć combo:

  • Get all errors of a szczególna forma
  • zwróć przetłumaczony błąd
  • weź pod uwagę pluralizację w razie potrzeby

Oto jest:

foreach ($form->getErrors() as $key => $error) {

   //If the message requires pluralization
    if($error->getMessagePluralization() !== null) {
        $errors[] = $this->container->get('translator')->transChoice(
            $error->getMessage(), 
            $error->getMessagePluralization(), 
            $error->getMessageParameters(), 
            'validators'
            );
    } 
    //Otherwise, we do a classic translation
    else {
        $errors[] = $this->container->get('translator')->trans(
            $error->getMessage(), 
            array(), 
            'validators'
            );
    }
}

Ta odpowiedź powstała z 3 różnych postów:

 4
Author: Mick,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-15 07:16:01

Możesz również użyć usługi walidatora, aby uzyskać naruszenia ograniczeń:

$errors = $this->get('validator')->validate($user);
 3
Author: antoinet,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-08-23 21:05:36

Przetłumaczone Komunikaty Błędów Formularza (Symfony2.3)

Moja wersja rozwiązania problemu:

/src / Acme/MyBundle/Resources/config / services.yml

services:
    form_errors:
        class: Acme\MyBundle\Form\FormErrors

/src / Acme/MyBundle/Form / FormErrors.php

<?php
namespace Acme\MyBundle\Form;

class FormErrors
{
    public function getArray(\Symfony\Component\Form\Form $form)
    {
        return $this->getErrors($form);
    }

    private function getErrors($form)
    {
        $errors = array();

        if ($form instanceof \Symfony\Component\Form\Form) {

            // соберем ошибки элемента
            foreach ($form->getErrors() as $error) {

                $errors[] = $error->getMessage();
            }

            // пробежимся под дочерним элементам
            foreach ($form->all() as $key => $child) {
                /** @var $child \Symfony\Component\Form\Form */
                if ($err = $this->getErrors($child)) {
                    $errors[$key] = $err;
                }
            }
        }

        return $errors;
    }
}

/src / Acme/MyBundle/Controller / DefaultController.php

$form = $this->createFormBuilder($entity)->getForm();
$form_errors = $this->get('form_errors')->getArray($form);
return new JsonResponse($form_errors);

W Symfony 2.5 można bardzo łatwo uzyskać błędy wszystkich pól:

    $errors = array();
    foreach ($form as $fieldName => $formField) {
        foreach ($formField->getErrors(true) as $error) {
            $errors[$fieldName] = $error->getMessage();
        }
    }
 3
Author: Lebnik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-21 08:02:42

Jeśli używasz własnych walidatorów, Symfony nie zwraca błędów generowanych przez te walidatory w $form->getErrors(). $form->getErrorsAsString() zwróci wszystkie potrzebne błędy, ale jego wynik jest niestety sformatowany jako łańcuch znaków, a nie tablica.

Metoda, której używasz, aby uzyskać wszystkie błędy (niezależnie od tego, skąd pochodzą), zależy od wersji Symfony, której używasz.

Większość proponowanych rozwiązań polega na stworzeniu funkcji rekurencyjnej, która skanuje wszystkie formy potomne i wydobywa odpowiednie błędy w jednej tablicy. Symfony 2.3 nie posiada funkcji $form->hasChildren(), ale posiada $form->all().

Oto Klasa pomocnicza Symfony 2.3, której możesz użyć do wyodrębnienia wszystkich błędów z dowolnego formularza. (Opiera się na kodzie z komentarza yapro na powiązanym zgłoszeniu błędu na koncie Github Symfony.)

namespace MyApp\FormBundle\Helpers;

use Symfony\Component\Form\Form;

class FormErrorHelper
{
    /**
     * Work-around for bug where Symfony (2.3) does not return errors from custom validaters,
     * when you call $form->getErrors().
     * Based on code submitted in a comment here by yapro:
     * https://github.com/symfony/symfony/issues/7205
     *
     * @param Form $form
     * @return array Associative array of all errors
     */
    public function getFormErrors($form)
    {
        $errors = array();

        if ($form instanceof Form) {
            foreach ($form->getErrors() as $error) {
                $errors[] = $error->getMessage();
            }

            foreach ($form->all() as $key => $child) {
                /** @var $child Form */
                if ($err = $this->getFormErrors($child)) {
                    $errors[$key] = $err;
                }
            }
        }

        return $errors;
    }
}

Kod wywoławczy:

namespace MyApp\ABCBundle\Controller;

use MyApp\FormBundle\Helpers;

class MyController extends Controller
{
    public function XYZAction()
    {
        // Create form.

        if (!$form->isValid()) {
            $formErrorHelper = new FormErrorHelper();
            $formErrors = $formErrorHelper->getFormErrors($form);

            // Set error array into twig template here.
        }
    }

}
 2
Author: Jay Sheth,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-28 16:28:26

Na podstawie odpowiedzi @ Jay Seth, zrobiłem wersję klasy FormErrors specjalnie dla formularzy Ajax:

// src/AppBundle/Form/FormErrors.php
namespace AppBundle\Form;

class FormErrors
{

    /**
     * @param \Symfony\Component\Form\Form $form
     *
     * @return array $errors
     */
    public function getArray(\Symfony\Component\Form\Form $form)
    {
        return $this->getErrors($form, $form->getName());
    }

    /**
     * @param \Symfony\Component\Form\Form $baseForm
     * @param \Symfony\Component\Form\Form $baseFormName
     *
     * @return array $errors
     */
    private function getErrors($baseForm, $baseFormName) {
        $errors = array();
        if ($baseForm instanceof \Symfony\Component\Form\Form) {
            foreach($baseForm->getErrors() as $error) {
                $errors[] = array(
                    "mess"      => $error->getMessage(),
                    "key"       => $baseFormName
                );
            }

            foreach ($baseForm->all() as $key => $child) {
                if(($child instanceof \Symfony\Component\Form\Form)) {
                    $cErrors = $this->getErrors($child, $baseFormName . "_" . $child->getName());
                    $errors = array_merge($errors, $cErrors);
                }
            }
        }
        return $errors;
    }
}

Użycie (np. w Twojej akcji):

$errors = $this->get('form_errors')->getArray($form);

Wersja Symfony: 2.8.4

Przykładowa odpowiedź JSON:

{
    "success": false,
    "errors": [{
        "mess": "error_message",
        "key": "RegistrationForm_user_firstname"
    }, {
        "mess": "error_message",
        "key": "RegistrationForm_user_lastname"
    }, {
        "mess": "error_message",
        "key": "RegistrationForm_user_email"
    }, {
        "mess": "error_message",
        "key": "RegistrationForm_user_zipCode"
    }, {
        "mess": "error_message",
        "key": "RegistrationForm_user_password_password"
    }, {
        "mess": "error_message",
        "key": "RegistrationForm_terms"
    }, {
        "mess": "error_message2",
        "key": "RegistrationForm_terms"
    }, {
        "mess": "error_message",
        "key": "RegistrationForm_marketing"
    }, {
        "mess": "error_message2",
        "key": "RegistrationForm_marketing"
    }]
}

Obiekt error zawiera pole "key", które jest identyfikatorem wejściowego elementu DOM, dzięki czemu można łatwo wypełniać komunikaty o błędach.

Jeśli masz formularze potomne wewnątrz rodzica, nie zapomnij dodać opcji cascade_validation wewnątrz formularza rodzica setDefaults.

 2
Author: RobbeR,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-07 09:17:22

SYMFONY 3X

Inne SF 3.Podane tutaj metody X nie działają dla mnie, ponieważ mogłem przesłać puste dane do formularza (ale nie mam ograniczeń null/NotBlanck). W tym przypadku łańcuch błędu będzie wyglądał następująco:

string(282) "ERROR: This value should not be blank.
ERROR: This value should not be blank.
ERROR: This value should not be blank.
ERROR: This value should not be blank.
ERROR: This value should not be blank.
ERROR: This value should not be null.
name:
    ERROR: This value should not be blank.
"
Co nie jest zbyt użyteczne. Więc zrobiłem to:
public function buildErrorArray(FormInterface $form)
{
    $errors = [];

    foreach ($form->all() as $child) {
        $errors = array_merge(
            $errors,
            $this->buildErrorArray($child)
        );
    }

    foreach ($form->getErrors() as $error) {
        $errors[$error->getCause()->getPropertyPath()] = $error->getMessage();
    }

    return $errors;
}

Który zwróci, że:

array(7) {
  ["data.name"]=>
  string(31) "This value should not be blank."
  ["data.street"]=>
  string(31) "This value should not be blank."
  ["data.zipCode"]=>
  string(31) "This value should not be blank."
  ["data.city"]=>
  string(31) "This value should not be blank."
  ["data.state"]=>
  string(31) "This value should not be blank."
  ["data.countryCode"]=>
  string(31) "This value should not be blank."
  ["data.organization"]=>
  string(30) "This value should not be null."
}
 2
Author: sbouba,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-07 17:51:19

W przypadku Symfony 2.1 do użytku z wyświetlaniem błędów Twig zmieniłem funkcję, aby dodać FormError zamiast po prostu je pobierać, w ten sposób masz większą kontrolę nad błędami i nie musisz używać error_bubbling na każdym pojedynczym wejściu. Jeśli nie ustawisz go w sposób poniżej {{form_errors (form)}} pozostanie puste:

/**
 * @param \Symfony\Component\Form\Form $form
 *
 * @return void
 */
private function setErrorMessages(\Symfony\Component\Form\Form $form) {      

    if ($form->count() > 0) {
        foreach ($form->all() as $child) {
            if (!$child->isValid()) {
                if( isset($this->getErrorMessages($child)[0]) ) {
                    $error = new FormError( $this->getErrorMessages($child)[0] );
                    $form->addError($error);
                }
            }
        }
    }

}
 1
Author: Hard-Boiled Wonderland,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-09 20:10:35

$form - >getErrors () działa dla mnie.

 1
Author: ahyong,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-28 07:21:30

Wymyśliłem to rozwiązanie. Działa solidnie z najnowszą Symfony 2.4.

Postaram się udzielić kilku wyjaśnień.

Używanie oddzielnego walidatora

Myślę, że to zły pomysł, aby używać oddzielnej walidacji do walidacji jednostek i zwracać wiadomości o naruszeniu ograniczeń, jak sugerują inni autorzy.

  1. Będziesz musiał ręcznie zweryfikować wszystkie encje, określić grupy walidacji, itp. Przy złożonych formach hierarchicznych nie jest praktyczny w ogóle i szybko wyjdzie z rąk.

  2. W ten sposób będziesz sprawdzał formularz dwa razy: raz z form i raz z oddzielnym walidatorem. To zły pomysł z perspektywy wydajności.

Sugeruję rekurencyjnie iterację typu formularza z jego dziećmi w celu zbierania komunikatów o błędach.

Używanie niektórych sugerowanych metod z wyłącznym wyrażeniem IF

Niektóre odpowiedzi sugerowane przez innych autorów zawierają wzajemnie wykluczające się stwierdzenia, jeśli w ten sposób: if ($form->count() > 0) lub if ($form->hasChildren()).

Z tego co widzę, każda forma może mieć błędy tak samo jak dzieci. Nie jestem ekspertem w komponencie Symfony Forms, ale w praktyce nie pojawią się pewne błędy samego formularza, takie jak błąd ochrony CSRF lub błąd dodatkowych pól . Proponuję usunąć tę separację.

Wykorzystanie denormalizowanej struktury wyników

Niektórzy autorzy sugerują umieszczenie wszystkich błędów wewnątrz zwykłej tablicy. Więc wszystkie komunikaty o błędach sama forma i jej dzieci zostaną dodane do tej samej tablicy z różnymi strategiami indeksowania: number-based dla błędów własnych typu I name-based dla błędów dzieci. Proponuję użyć znormalizowanej struktury danych formularza:

errors:
    - "Self error"
    - "Another self error"

children
    - "some_child":
        errors:
            - "Children error"
            - "Another children error"

        children
            - "deeper_child":
                errors:
                    - "Children error"
                    - "Another children error"

    - "another_child":
        errors:
            - "Children error"
            - "Another children error"

W ten sposób wynik można łatwo później iterować.

Moje rozwiązanie

Oto moje rozwiązanie tego problemu:

use Symfony\Component\Form\Form;

/**
 * @param Form $form
 * @return array
 */
protected function getFormErrors(Form $form)
{
    $result = [];

    // No need for further processing if form is valid.
    if ($form->isValid()) {
        return $result;
    }

    // Looking for own errors.
    $errors = $form->getErrors();
    if (count($errors)) {
        $result['errors'] = [];
        foreach ($errors as $error) {
            $result['errors'][] = $error->getMessage();
        }
    }

    // Looking for invalid children and collecting errors recursively.
    if ($form->count()) {
        $childErrors = [];
        foreach ($form->all() as $child) {
            if (!$child->isValid()) {
                $childErrors[$child->getName()] = $this->getFormErrors($child);
            }
        }
        if (count($childErrors)) {
            $result['children'] = $childErrors;
        }
    }

    return $result;
}
Mam nadzieję, że to komuś pomoże.
 1
Author: Slava Fomin II,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-28 19:23:04

Dla Symfony 3.2 i powyżej użyj tego,

public function buildErrorArray(FormInterface $form)
{
    $errors = array();

    foreach ($form->getErrors() as $key => $error) {
        if ($form->isRoot()) {
            $errors['#'][] = $error->getMessage();
        } else {
            $errors[] = $error->getMessage();
        }
    }

    foreach ($form->all() as $child) {
        if (!$child->isValid()) {
            $errors[$child->getName()] = (string) $child->getErrors(true, false);
        }
    }
    return $errors;
}

Użyj str_replace , Jeśli chcesz pozbyć się irytującego tekstu " Error: " w każdym tekście opisu błędu.

$errors[$child->getName()] = str_replace('ERROR:', '', (string) $child->getErrors(true, false));
 1
Author: Anjana Silva,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-24 10:13:13

Dla Symfony 2.1:

To jest moje ostateczne rozwiązanie łączące wiele innych rozwiązań:

protected function getAllFormErrorMessages($form)
{
    $retval = array();
    foreach ($form->getErrors() as $key => $error) {
        if($error->getMessagePluralization() !== null) {
            $retval['message'] = $this->get('translator')->transChoice(
                $error->getMessage(), 
                $error->getMessagePluralization(), 
                $error->getMessageParameters(), 
                'validators'
            );
        } else {
            $retval['message'] = $this->get('translator')->trans($error->getMessage(), array(), 'validators');
        }
    }
    foreach ($form->all() as $name => $child) {
        $errors = $this->getAllFormErrorMessages($child);
        if (!empty($errors)) {
           $retval[$name] = $errors; 
        }
    }
    return $retval;
}
 0
Author: Fernando P. G.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-22 14:53:54

SYMFONY 3.1

Po prostu zaimplementowałem statyczną metodę obsługi wyświetlania błędów

static function serializeFormErrors(Form\Form $form)
{
    $errors = array();
    /**
     * @var  $key
     * @var Form\Form $child
     */
    foreach ($form->all() as $key => $child) {
        if (!$child->isValid()) {
            foreach ($child->getErrors() as $error) {
                $errors[$key] = $error->getMessage();
            }
        }
    }

    return $errors;
}

Mając nadzieję pomóc

 0
Author: Shigiang Liu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-02 15:26:39