【问题标题】:Symfony2 - UniqueEntity not working ignoreNull with two fieldsSymfony2 - UniqueEntity 不工作 ignoreNull 有两个字段
【发布时间】:2017-02-14 06:11:01
【问题描述】:

Symfony2 - UniqueEntity 不能与字段一起使用 ignoreNull

// src/AppBundle/Entity/Service.php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * @ORM\Entity
 * @UniqueEntity(
 *     fields={"host", "port"},
 *     errorPath="port",
 *     message="This port is already in use on that host."
 * )
 */
class Service
{
    /**
     * @ORM\ManyToOne(targetEntity="Host")
     */
    public $host;

    /**
     * @ORM\Column(type="integer", nullable = true)
     */
    public $port;
}

我尝试使用空值添加端口,它抛出“该端口已在该主机上使用。”错误。

我想在端口为空时忽略验证。

【问题讨论】:

    标签: php validation symfony


    【解决方案1】:

    查看此链接以获取解决方案。 UniqueEntity validation in symfony 2

    ignoreNull¶

    类型:布尔默认值:true

    如果此选项设置为 true,则该约束将允许多个实体对一个字段具有空值,而不会失败验证。如果设置为 false,则只允许一个空值 - 如果第二个实体也有一个空值,则验证将失败。

    【讨论】:

    • 我不想添加host但port=null多个条目,它不允许
    猜你喜欢
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    相关资源
    最近更新 更多