Created
May 12, 2011 16:19
-
-
Save extolabs/968857 to your computer and use it in GitHub Desktop.
Form Errors Symfony
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
private function getAllFormErrors($children, $template = true) { | |
foreach ($children as $child) { | |
if ($child->hasErrors()) { | |
$vars = $child->createView()->getVars(); | |
$errors = $child->getErrors(); | |
foreach ($errors as $error) { | |
$this->allErrors[$vars["name"]][] = $this->convertFormErrorObjToString($error); | |
} | |
} | |
if ($child->hasChildren()) { | |
$this->getAllErrors($child); | |
} | |
} | |
} | |
public function getAllErrors($children, $template = true) { | |
$this->getAllFormErrors($children); | |
return $this->allErrors; | |
} | |
private function convertFormErrorObjToString($error) { | |
$errorMessageTemplate = $error->getMessageTemplate(); | |
foreach ($error->getMessageParameters() as $key => $value) { | |
$errorMessageTemplate = str_replace($key, $value, $errorMessageTemplate); | |
} | |
return $errorMessageTemplate; | |
} |
Hello are you aware of 'error_bubbling'?
e.g. (within your Form builder)
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('title', 'text', array('error_bubbling' => true))
->add('body', 'textarea', array('error_bubbling' => true))
Then your form parent has access to the errors:
$form->getErrors();
Further info: http://stackoverflow.com/questions/6978723/symfony2-how-to-get-form-validation-errors-after-binding-the-request-to-the-fo
$errors = array();
foreach ($form->getChildren() as $child) {
if ($child->hasErrors()) {
foreach ($child->getErrors() as $error) {
$errors[$child->getName()][] = $error->getMessageTemplate();
}
}
}
Get Translated Form Error Messages (Symfony2.3)
My version of solving the problem:
/src/Intranet/OrgunitBundle/Resources/config/services.yml
services:
form_errors:
class: Intranet\OrgunitBundle\Form\FormErrors
/src/Intranet/OrgunitBundle/Form/FormErrors.php
<?php
namespace Intranet\OrgunitBundle\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/Intranet/OrgunitBundle/Controller/DefaultController.php
$form = $this->createFormBuilder($entity)->getForm();
$form_errors = $this->get('form_errors')->getArray($form);
return new JsonResponse($form_errors);
Thanks @yapro It works!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The above Form.php file is
Symfony\Components\Form\Form.php