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

问题描述 投票:0回答:1

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

/**
 * @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类型与验证一起使用,有什么解决方案吗?

symfony validation assert
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.