API 平台/Symfony 的类型验证问题

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

我正在尝试对类型使用验证以产生个性化消息错误。

我使用 API Platform v3.1.11 和 Symfony 6.2.10

我把参数设置为禁用类型强制:

#[ApiResource(
    denormalizationContext: ['disable_type_enforcement' => true]
)]
    #[ORM\Column]
    #[Assert\NotBlank(message:"Sender zipcode is required.")] 
    #[Assert\Type(type:"int",message:"Sender zipcode is in wrong format.")]   
    private ?int $sender_zipcode = null;

    public function getSenderZipcode(): ?int
    {
        return $this->sender_zipcode;
    }

    public function setSenderZipcode($sender_zipcode): self
    {
        $this->sender_zipcode = $sender_zipcode;

        return $this;
    }

当我将带有字符串值的 POST 请求放入 sender_zipcode 时,我收到此消息错误

    "hydra:description": "Failed to denormalize attribute \"senderZipcode\" value for class \"App\\Entity\\Shipment\": Expected argument of type \"?int\", \"string\" given at property path \"senderZipcode\".",

而不是“发件人邮政编码格式错误。”

谢谢

php symfony api-platform.com symfony6
© www.soinside.com 2019 - 2024. All rights reserved.