symfony 2 中的 UniqueEntity 验证 [英] UniqueEntity validation in symfony 2

查看:25
本文介绍了symfony 2 中的 UniqueEntity 验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库表如下:

I have a database table as below:

 payment_method
 --------------
 pk id
 fk payer_id
    payment_method
    bank_name

这里 payer_id 可以有空值.我有一个如下所示的 UniqueEntity

Here payer_id can have null value. I have a UniqueEntity as below

* @UniqueEntity(
*     fields={"paymentMethod","payer","bank"},
*     errorPath="payer",
*     message="This payment source is already in use.",
*     groups={"persist"}
* )

如果 payer_id 不为空,此验证工作正常.它不适用于以下情况:

This validation works fine if payer_id is not null. It does not work for following cases:

 id    payer_id    bank_name  payment_method
 1                 US Bank      credit card
 2                 US Bank      credit card

我该如何解决这个问题?

How can i fix this ?

推荐答案

设置 ignoreNull=false.如果设置为 false,则只允许一个空值 - 如果第二个实体也有空值,则验证将失败.

Set ignoreNull=false. If set to false, only one null value is allowed - if a second entity also has a null value, validation would fail.

http://symfony.com/doc/current/reference/约束/UniqueEntity.html#ignorenull

这篇关于symfony 2 中的 UniqueEntity 验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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