gpt4 book ai didi

php - Symfony 断言日期时间 JMSSerializer

转载 作者:搜寻专家 更新时间:2023-10-31 21:26:28 24 4
gpt4 key购买 nike

我有端点 postOrder - 创建实体订单。在实体顺序中,我有 DateTime 类型的字段,我想当有人写字符串而不是 DateTime 时,我有 "Not valid, should be DateTime"。对于我这样使用的其他领域

 * @Assert\Length(min=3, max=255)

 * @Assert\Regex(
* pattern= "/^[\d() \-+]+$/",
* message= "This text cannot contain numbers"
* )

     * @Assert\NotBlank()

我得到所有请求,然后在验证时为具体实体序列化然后反序列化,并且在端点中有来自断言的信息,但对于 DateTime 这不起作用我使用 FosRestBundle 和 JMSSerializer 这是我的操作

 /**
* Post Order.
*
* @ApiDoc(
* resource = true,
* description = "Post Order",
* parameters={
* {"name"="comment", "dataType"="string", "required"=false, "description"="comment"},
* {"name"="interview_date", "dataType"="date", "required"=false, "description"="date conect for developer"},
* {"name"="contact_date", "dataType"="date", "required"=false, "description"="date contact fir TIM"}
*
* },
* statusCodes = {
* 200 = "Returned when successful",
* 400 = "Returned secret token is not valid"
* },
* section="Order"
* )
*
* @RestView()
*
* @param Request $request
*
* @return View
*
* @throws NotFoundHttpException when not exist
*/
public function postOrderAction(Request $request)
{
$data = $request->request->all();
$data = $this->get('serializer')->serialize($data, 'json');
$serviceLead = $this->get('serializer')->deserialize($data, 'Artel\ProfileBundle\Entity\CodeServiceLead', 'json');
$errors = $this->get('validator')->validate($serviceLead);
if (count($errors) > 0) {
$view = $this->view($errors, 400);

return $this->handleView($view);
}

和字段

class Orders
{


/**
* @var string
*
* @ORM\Column(name="comment", type="string", nullable=true)
* @Groups({"get_all_orders_admin", "get_all_orders", "for_vip"})
*/
protected $comment;

/**
* @var \DateTime
* @ORM\Column(name="interview_date", type="date", nullable=true)
* @Groups({"get_all_orders_admin", "get_all_orders", "for_vip"})
* @Assert\DateTime()
*/
protected $interview_date;

/**
* @var \DateTime
* @ORM\Column(name="contact_date", type="date", nullable=true)
* @Groups({"get_all_orders_admin", "get_all_orders", "for_vip"})
* @Assert\DateTime()
*/
protected $contact_date;

现在当我尝试反序列化实体订单时出现错误

{
"error": {
"code": 500,
"message": "Internal Server Error",
"exception": [
{
"message": "Invalid datetime \"some_string\", expected format Y-m-d\\TH:i:sO.",
"class": "JMS\\Serializer\\Exception\\RuntimeException",

在这种情况下,如何在没有 500 的情况下返回正确的错误或断言?

最佳答案

当您调用 deserialize() 时,自 JMS 序列化器 already integrates with the Doctrine ORM 以来,它已经根据您的 Doctrine 注释检查您的实体的有效性。 .

如果反序列化失败,一个exception is thrown ,这就是您所看到的。

如果您想自己处理,您需要做的就是将代码放在 try/catch block 中:

public function postOrderAction(Request $request)
{
$data = $request->request->all();
$data = $this->get('serializer')->serialize($data, 'json');

try {
$serviceLead = $this->get('serializer')->deserialize(
$data,
'Artel\ProfileBundle\Entity\CodeServiceLead',
'json'
);
} catch (\Exception $e) {
$view = $this->view((array) $e->getMessage(), 400);

return $this->handleView($view);
}
}

我没有看到您的 view() 函数,但我假设它需要一个错误消息字符串数组,所以我将异常消息转换为一个数组。无论哪种方式,您都会明白这一点。

关于php - Symfony 断言日期时间 JMSSerializer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35272797/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com