, (*1)
, (*2)
This bundle contains two things:
- a service that exports all errors from form to array
- Twig extension that helps display them as list, (*3)
There already is form method getErrorsAsString(), but it does not provide you with labels. It also requires you to write boilerplate code in controllers, or writing your own service. If you will try to list all errors by yourself, you will notice that $form->getErrors() returns errors only for Form itself, not for particular fields or subforms. Why bother creatng your own parser? Use mine :), (*4)
DISCLAIMER
I wrote this bundle basing on Symfony 2.3.13 for my own purposes, did not tested on other versions, but having in mind what Fabien said about backwards compatibility, it should work at least on 2.4 and any future relase of Symfony2, as well as on previous versions. Remember that I don't guarantee this, so feel free to test, fork and make your changes., (*5)
INSTALLATION
-
Add this to require part of your composer.json file:, (*6)
"ex3v/formerrorsbundle": "dev-master"
-
Add this to your AppKernel.php (under /app), (*7)
new Ex3v\FormErrorsBundle\Ex3vFormErrorsBundle(),
then run composer update
on your project to install., (*8)
USAGE
While in controller, add not only FormView, but also Form object to returning array:, (*9)
return array(
'form' => $form->createView(),
'formFull' => $form
);
Having this, you can call new method in Twig:, (*10)
{{ all_form_errors(formFull) }}
This method will (by default) display all of your form errors as <ul> list, like this:, (*11)
<ul>
<li>Title: field cannot be empty.</li>
<li>Website: this is not a valid URL.</li>
</ul>
You can customize your output by adding additional parameters:, (*12)
{{ all_form_errors(formFull, "div", "myclass") }}
This will produce errors list in which each error is wrapped in separate div, having "myclass" as its class:, (*13)
<div class="myclass">
Title: field cannot be empty.
</div>
<div class="myclass">
Website: this is not a valid URL.
</div>
If you want more control over your errors, you also can call service that parses errors directly in controller:, (*14)
$formErrorsParser = $this->get('formErrorsParser');
$errors = $formErrorsParser->parseErrors($form);
$errors array will contain arrays composed of:
- field name
- field label
- Symfony\Component\Form\FormError error object, that contains error message in two formats (original and translated) as well as field value., (*15)
Any suggestions or contributions are warmly welcome. Happy coding!, (*16)