Symfony Assert DateTime JMSSerializer

时间:2016-02-08 15:06:47

标签: php symfony datetime fosrestbundle jmsserializerbundle

我有端点postOrder - 创建实体订单。在实体顺序中,我有类型为DateTime的字段,我希望当有人写字符串而不是DateTime我有"无效时,应该是DateTime" 。对于我使用的其他字段

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

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

     * @Assert\NotBlank()

我获取所有请求然后序列化然后反序列化为具体实体验证并从端点中的assert获取信息,但对于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的情况下返回正确的错误或断言?

1 个答案:

答案 0 :(得分:1)

当您致电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()函数,但我认为它期待一组错误消息字符串,所以我将异常消息转换为数组。无论哪种方式,你都能理解。