Symfony 断言日期时间 JMSSerializer

Symfony Assert DateTime JMSSerializer

我有端点 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",

在这种情况下,如何 return 更正错误或断言,没有 500?

当您调用 deserialize() 时,自 JMS 序列化程序 already integrates with the Doctrine ORM.

以来,它已经根据您的 Doctrine 注释检查您的实体的有效性

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

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

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() 函数,但我假设它需要一个错误消息字符串数组,所以我将异常消息转换为一个数组。无论哪种方式,您都会明白。