Symfony-验证:属性的类型必须为bool,指定字符串

时间:2020-04-20 18:56:04

标签: symfony validation assert

我有一个实体和一个布尔类型的列

/**
 * @ORM\Column(type="boolean", nullable=false)
 * @Assert\NotBlank()
 */
private $isActive;

当我尝试向此列添加字符串(仅用于测试)时,出现此错误消息

The type of the attribute must be bool, string given

因此,我添加了验证类型

* @Assert\Type(
*     type="boolean",
*     message="The value {{ value }} is not a valid {{ type }}."
* )

但总是会启动消息错误,因此,我尝试通过创建自己的资产验证来尝试第二种解决方案

if(false === is_bool($user->getIsActive())){
   $this->context->buildViolation($constraint->message)->atPath('isActive')->addViolation();
} 

但总是粉碎代码并显示消息。

PS:如果我将列类型更改为字符串验证工作正常,但是我想将bool类型与验证一起使用,有什么解决方案吗?

1 个答案:

答案 0 :(得分:0)

我通过添加以下行来解决此问题:

/**
 * @ApiResource(
 *     denormalizationContext={
 *        "disable_type_enforcement"=true
 *     }
 * )
 */