Php 为什么我会收到“;此值应为“字符串”类型;在Symfony 5上使用日期时间约束时?

Php 为什么我会收到“;此值应为“字符串”类型;在Symfony 5上使用日期时间约束时?,php,symfony,api-platform.com,symfony-validator,symfony5,Php,Symfony,Api Platform.com,Symfony Validator,Symfony5,我拥有以下实体(仅随附相关部分): 使用apipplatform\Core\Annotation\apirource; 使用条令\ORM\Mapping作为ORM; 使用Symfony\Component\Validator\Constraints作为断言; /** *@ApiResource(mercure=true) *@ORM\Entity(repositoryClass=“App\Repository\EventRepository”) */ 班级活动{ /** *@ORM\Column

我拥有以下实体(仅随附相关部分):

使用apipplatform\Core\Annotation\apirource;
使用条令\ORM\Mapping作为ORM;
使用Symfony\Component\Validator\Constraints作为断言;
/**
*@ApiResource(mercure=true)
*@ORM\Entity(repositoryClass=“App\Repository\EventRepository”)
*/
班级活动{
/**
*@ORM\Column(type=“datetime”)
*@Assert\DateTime
*@Assert\NotNull
*/
私人$createdAt;
公共函数构造(){
$this->createdAt=new\DateTime();
}
公共函数getCreatedAt():?\DateTimeInterface{
返回$this->createdAt;
}
公共函数setCreatedAt(\DateTimeInterface$createdAt):self{
$this->createdAt=$createdAt;
退还$this;
}
}
其存储库:

class EventRepository extends ServiceEntityRepository {
    public function __construct(ManagerRegistry $registry) {
        parent::__construct($registry, Event::class);
    }
}
创建对事件端点的POST请求(通过Postman或Swagger UI)时,会失败,出现以下异常:


您使用了错误的断言

日期
。而
DateTimeInterface
两者都不是

您应该使用
类型


使用
Assert\Date
验证
DateTime
对象的功能在Symfony 4.2上被弃用,而且。

@delboy1978uk我使用的是Api平台,它会自动插入。
{“name”:“test”、“@creator”:“/people/23”、“description”:“desc”}/code>尽管这与此无关,因为错误在构造函数中初始化的
createdAt
属性处。我想知道,如果您有这个问题(它至少可以消除复杂性),您是否要切断ApiPlatform,自己创建一个
事件
并手动验证它。也许这是ApiPlatform加载对象的方式?嗯,我有另一个实体(未连接到API平台),它包含一个具有相同约束的日期时间,并按预期工作。我们使用的是AP2.1,我记得它可能是如何生成对象的(已经有一段时间了)?你可以进入vendor中的验证器文件,然后
dump()
它要检查什么,这就是我要开始的地方。啊!我们在某些地方有
@Assert\Type(“datetime”)
,这是有道理的。这对我来说是全新的!从那时起,我一直在使用
@Assert\DateTime(groups={“new”})
,现在我在新的symfony 5.1项目上收到了那个奇怪的验证器消息。现在我改为上面给出的例子,它是有效的。令人惊叹的!谢谢如果您需要自定义断言,这里有一个示例:@Assert\Type(Type=“\DateTimeInterface”,message=“Custom message”)
/**
 * @Assert\Type("\DateTimeInterface")
 */
 private $createdAt;