Symfony 表单,错误冒泡 [英] Symfony forms, error bubbling

查看:26
本文介绍了Symfony 表单,错误冒泡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了表单错误冒泡的问题.我的表单中的一个字段定义如下:

I have a problem with forms' error bubbling. One field in my form is defined like this:

$formBuilder->add('title','text',
   'required'  => true, 
   'error_bubbling' => false,
   )
)

我想在此字段中添加这样的验证器:

I would like to add a validator like this to this field:

/**
  * @Assert\True(message = "Bad title.")
  */
public function getTitleCorrect()
{
    /* ... */     
    return false;
} 

工作正常,但错误消息显示在表单顶部,而不是字段行中.

It works ok, but the error message shows up on top of the form, not in the field row.

在 Twig 模板中,此错误消息由 {{form_errors(form)}} 呈现为全局错误.当我使用 {{form_errors(form.title)}} 时,它不会打印我的错误.

In the Twig template this error message is rendered by {{form_errors(form)}} as a global error. When I use {{form_errors(form.title)}}, it does not print my error.

如何更改错误的分配?

推荐答案

仅当验证器附加到相应的属性时,消息才会附加到字段.您的验证器附加到类的方法上,因此错误确实是全局的.

Messages are attached to a field only when validator is attached to corresponding property. Your validator is attached to a method of the class so error is indeed global.

你应该这样做:

use ...\TitleValidator as AssertTitleValid;

class MyEntity
{
    /**
     * @AssertTitleValid
     */
    private $title;
}

并创建您自己的 TitleValidator 类.

And create your own TitleValidator class.

这篇关于Symfony 表单,错误冒泡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆