【问题标题】:UniqueEntity on multiple fields doesn't show an error message多个字段上的 UniqueEntity 不显示错误消息
【发布时间】: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


【解决方案1】:

由于 UniqueEntity 验证似乎在您的一个子表单中失败...

(因为错误信息只出现在getErrorsAsString 而不是getErrors

...为了让父表单中的子表单错误可用,请使用错误冒泡:

$formBuilder->add('child-form','child-form-type', array(
       'error_bubbling' => true,
   )
)

...或在您的子表单中:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'error_bubbling' => true,
    ));
}

【讨论】:

  • error_bubling 在我的全局表单上不起作用,我必须将它添加到每个子表单中,为什么?
  • 使用 ->add() 还是使用 setDefaultOptions() ?当然,子表单中的 setDefaultOptions - 编辑了答案以使其更加清晰。错误冒泡“通知”父表单它的错误
  • 谢谢。在我看来,error_bubbling 默认应该设置为 true。
【解决方案2】:

有时会出现此问题,因为实体未将错误消息绑定到正确的字段。使用validation.yml 文件可以让您更好地控制错误消息的处理方式和位置。

# src/Ineat/LinkQuestionBundle/Resources/config/validation.yml
Ineat\LinkQuestionBundle\Entity\LinkRepository:
    constraints:
        - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
            fields: [firstForm, firstQuestion, lastForm, lastQuestion]
            errorPath: lastQuestion
            message: 'This port is already in use on that host.'

更多信息在这里:Symfony - UniqueEntity

将文本字符串放入“翻译”文件也是一种很好的做法(即使您只使用/只有一种语言)。您的views / entities / forms / controllers ... 中不会有一些文本,它们都将在您的翻译文件夹中,在一个位置。如有必要,也可以一次性更改重复的字符串。它看起来像这样:

# src/Ineat/LinkQuestionBundle/Resources/config/validation.yml
Ineat\LinkQuestionBundle\Entity\LinkRepository:
    constraints:
        - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
            fields: [firstForm, firstQuestion, lastForm, lastQuestion]
            errorPath: lastQuestion
            message: 'linkquestion.form.errors.unique'

# src/Ineat/LinkQuestionBundle/Resources/translations/validators.fr.yml
linkquestion:
  form:
    errors:
      unique: "This port is already in use on that host."
# Or which ever structure you choose.

然后告诉您的应用程序,它将始终使用法语。

【讨论】:

    猜你喜欢
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    • 1970-01-01
    • 2011-10-08
    • 2011-02-03
    • 2012-01-16
    • 1970-01-01
    相关资源
    最近更新 更多