繁体   English   中英

Symfony Assert DateTime JMSSerializer

[英]Symfony Assert DateTime JMSSerializer

我有端点postOrder - 创建实体订单。 在实体顺序中我有类型DateTime的字段,我希望当有人写字符串而不是DateTime我有“无效,应该是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 Serializer 已经与Doctrine ORM集成 ,因此已经在检查您的实体是否符合您的Doctrine注释的有效性。

如果反序列化失败, 则抛出异常 ,这就是您所看到的。

在您的情况下,您需要做的就是将代码放在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()函数,但我认为它期待一个错误消息字符串数组,所以我将异常消息转换为数组。 无论哪种方式,你都明白了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM