GithubHelp home page GithubHelp logo

formerrorsbundle's Introduction

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!

formerrorsbundle's People

Contributors

crayner avatar ex3v avatar friartuck6000 avatar k0pernikus avatar orthographic-pedant avatar stas-p avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

formerrorsbundle's Issues

How to translate the label?

I don't know how to translate the label. So far i have:

        ->add('lastname', 'text', array(
            'label' => 'form.last_name',
            'attr' => [
                'placeholder' => 'form.last_name'
            ]
        ))

The placeholder is translated correct in my input field, but in the error message the label is not translated. The error message itself is translated correct. What is the issue here?

Error: FormErrorsExtension::__construct() must be an instance of Ex3v\FormErrorsBundle\Twig\Translator

I have symfony 2.7 and I have this error
ContextErrorException in FormErrorsExtension.php line 27: Catchable Fatal Error: Argument 2 passed to Ex3v\FormErrorsBundle\Twig\FormErrorsExtension::__construct() must be an instance of Ex3v\FormErrorsBundle\Twig\Translator

You should change the class FormErrorsExtension construct to:

  public function __construct(FormErrorsParser $parser, TranslatorInterface $trans)
  {
        $this->parser = $parser;
        $this->trans =  $trans ;
  }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.