Adding a form error in an EventListener

风格不统一 提交于 2019-12-06 01:50:21

In this case you should use Validation Groups based on Submited Data. This method available since symfony 2.1.

And you don't need to pull events. Look here:

forms - http://symfony.com/doc/current/book/forms.html#groups-based-on-submitted-data

validation - http://symfony.com/doc/current/book/validation.html#validation-groups

Try this approach. And you should get code like this:

Entity script with validators: src/Acme/AcmeBundle/Entity/Url.php

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

...
    /**
     * @ORM\Column(name="port", type="integer")
     * @Assert\NotBlank(groups={"validation_partial", "validation_full"})
     */
    private $port;

    /**
     * @ORM\Column(name="protocol", type="string", length=10)
     * @Assert\NotBlank(groups={"validation_full"})
     */
    private $protocol;
...

Form script: src/Acme/AcmeBundle/Form/UrlType.php

use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

...

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'validation_groups' => function(FormInterface $form) {
            $data = $form->getData();

            if ('port' == $data->getType()) {
                return array('validation_full');
            } else {
                return array('validation_partial');
            }
        },
    ));

 }

Ok, I will try answer on your question in detail. For example. We have FormType like this:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name') // Some text field 'Name'
    ;

    $builder->addEventListener(FormEvents::PRE_BIND, function(FormEvent $event) use ($options) {
        $event->getForm()->get('name')->addError(new FormError('*** ERROR PRE_BIND'));
    });
    $builder->addEventListener(FormEvents::BIND, function(FormEvent $event) use ($options) {
        $event->getForm()->get('name')->addError(new FormError('*** ERROR BIND'));
    });
    $builder->addEventListener(FormEvents::POST_BIND, function(FormEvent $event) use ($options) {
        $event->getForm()->get('name')->addError(new FormError('*** ERROR POST_BIND'));
    });
}

You are right. If add errors in listeners for events: PRE_BIND, BIND, POST_BIND. You will get only errors from BIND and POST_BIND events. To understand why this is so you need to know 2 points.

First thing: Every element in form is also form. In our case our main form has children 'Name'(text element) which is also a form.

[MainForm]

-> [NameForm]

// there can be additional forms if your form has another elements

Second thing: When you bind request to a MainForm you invoke bind() function.

And this function invoke bind() function for every child of MainForm.

Answer for your question is in algorithm of this function. bind() function algorithm:

function bind($submittedData) {
    1) clear all errors
    2) dispatch event PRE_BIND
    3) invoke bind() function for children
    4) dispatch event BIND
    5) dispatch event POST_BIND
}

So based of our example programm flow will be:

Invoke bind() function for MainForm
1) MainForm - clear all errors
2) MainForm - dispatch event PRE_BIND // there our listener add error for child NameForm.
3) MainForm - invoke bind() function for child NameForm:
    1) NameForm - clear all errors // is the answer for your question, error from event MainForm:PRE_BIND cleared!!!
    2) NameForm - dispatch event PRE_BIND // no changes
    3) NameForm - invoke bind() for children // this form does not have children, so will be passed
    4) NameForm - dispatch event BIND // no changes
    5) NameForm - dispatch event POST_BIND // no changes
4) MainForm - dispatch event BIND // there our listener add error to child NameForm
5) MainForm - dispatch event POST_BIND // there our listener add another error to child NameForm.

I hope this explanation is helpful for you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!