Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Formerrorsbundle Laravel Package

ex3v/formerrorsbundle

FormErrorsBundle for Symfony exports all form errors, including field and subform labels, to an array and provides a Twig extension to render them as a customizable list. Avoid boilerplate parsers and display complete validation errors in templates with all_form_errors(form).

View on GitHub
Deep Wiki
Context7

FormErrorsBundle

knpbundles.com

Latest Stable Version Total Downloads License

This bundle contains two things:

  • a service that exports all errors from form to array
  • Twig extension that helps display them as list

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 :)

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.

INSTALLATION

  • Add this to require part of your composer.json file:

      "ex3v/formerrorsbundle": "dev-master"
    
  • Add this to your AppKernel.php (under /app)

      new Ex3v\FormErrorsBundle\Ex3vFormErrorsBundle(),
    

then run composer update on your project to install.

USAGE

While in controller, add not only FormView, but also Form object to returning array:

    return array(
        'form' => $form->createView(), 
        'formFull' => $form
    );

Having this, you can call new method in Twig:

    {{ all_form_errors(formFull) }}

This method will (by default) display all of your form errors as <ul> list, like this:

     <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:

    {{ 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:

    <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:

    $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.

Any suggestions or contributions are warmly welcome. Happy coding!

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
pharaonic/laravel-notification-dependencies
beautinow/translug
dev-brainiac/bot-cloaked
octoberrain/html
octoberrain/extension
octoberrain/exception
octoberrain/contracts
frankyso/ipaymu
3mdikram/worldmoneywords
snowsoft/config
itmosoft/laravel-subscriptions
larva/laravel-ai-qwen
adachsoft/api-version-bundle
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox
pairus/product-data
fayne/laravel-filament-cms-core