【发布时间】:2013-06-25 12:51:02
【问题描述】:
我已向我的实体添加了以下约束:
* @ORM\Table(name="link", uniqueConstraints={@ORM\UniqueConstraint(name="unique_link_idx", columns={"first_form_id", "first_question_id","last_form_id","last_question_id"})})
* @UniqueEntity(fields={"firstForm", "firstQuestion","lastForm","lastQuestion"}, message="Cette liaison existe déjà !")
* @ORM\Entity(repositoryClass="Ineat\LinkQuestionBundle\Entity\LinkRepository")
UniqueEntity 约束效果很好,因为在添加它之前,当尝试将完全相同的实体插入数据库时,我遇到了 DBALException abount 完整性约束。
从那以后我再也没有这个异常了,表单没有验证但{{ form_errors(form) }} 没有打印任何消息。
签入控制器后$form->getErrors() 返回一个空数组,而$form->getErrorsAsString() 包含UniqueENTity 约束。
如何显示我的 UniqueEntity 的约束错误消息
【问题讨论】:
-
getErrorsAsString()访问与getErrors()相同的属性,即 Symfony/Component/Form/Form 中的this->errors,因此$form->getErrors()返回一个空数组不是可能的。看看github.com/symfony/symfony/blob/master/src/Symfony/Component/… -
不,它只访问 $this->children 而 getErrors 只访问错误。
-
那么 uniqueEntity 约束来自您的子表单之一……您应该这么说。
标签: validation symfony doctrine-orm constraints unique-constraint