在EventListener中添加表单错误

lif*_*ifo 3 php symfony symfony-2.2

我有一个基于Symfony 2.2的应用程序,其表单的字段只需要基于表单中的另一个字段.我绑定了一个EventListener来捕获表单的提交时间,这样我就可以验证提交表单时是否真的不需要'required'字段.

我注意到我无法FormErrorPRE_BIND表单事件中设置内容.这样做不会显示错误,但是如果我绑定到BIND事件监听器,那么表单错误会正确显示但我不想等到BIND事件检查我的错误(我不希望潜在的坏数据绑定到我的实体).

有人能告诉我为什么会这样吗?

public function buildForm(FormBuilderInterface $builder, array $options)
{
    // snip ...

    $builder->addEventListener(FormEvents::PRE_BIND, function(FormEvent $event) use ($options) {
        $data = $event->getData();
        $form = $event->getForm();
        if ($data === null) {
            return;
        }

        // yes, this is definitely called; If I remove the if() and just
        // and just add the formError it still doesn't work.
        if ($data['type'] == 'port' and empty($data['protocol'])) {
            $form->get('protocol')->addError(new FormError('A valid protocol must be selected.'));
        }

    });

}
Run Code Online (Sandbox Code Playgroud)

QAr*_*rea 6

在这种情况下,您应该使用基于Submited Data的验证组.这个方法自symfony 2.1起可用.

而且你不需要拉事件.看这里:

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

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

试试这种方法.你应该得到这样的代码:

带验证器的实体脚本: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;
...
Run Code Online (Sandbox Code Playgroud)

表单脚本: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');
            }
        },
    ));

 }
Run Code Online (Sandbox Code Playgroud)

好的,我会详细回答你的问题.例如.我们有这样的FormType:

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'));
    });
}
Run Code Online (Sandbox Code Playgroud)

你是对的.如果在事件的侦听器中添加错误:PRE_BIND,BIND,POST_BIND.您将只从BIND和POST_BIND事件中获取错误.要理解为什么会这样,你需要知道2分.

第一件事: 形式中的每个元素也是形式.在我们的例子中,我们的主窗体有子'Name'(文本元素),它也是一个表单.

[MainForm的]

- > [NameForm]

//如果您的表单有其他元素,则可以有其他表单

第二件事: 当你将请求绑定到MainForm时,你调用bind()函数.

此函数为MainForm的每个子节点调用bind()函数.

您的问题的答案在于此功能的算法.bind()函数算法:

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
}
Run Code Online (Sandbox Code Playgroud)

所以基于我们的示例程序流程将是:

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.
Run Code Online (Sandbox Code Playgroud)

我希望这个解释对你有所帮助.