shuba.ivan February 2016

Symfony Assert DateTime JMSSerializer

I have endpoint postOrder - create entity order. In entity order I have fields with type DateTime and I want when somebody write string instead DateTime I have "Not valid, should be DateTime" . For other fields I use like this

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

or

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

or

     * @Assert\NotBlank()

I get all request then serialize then deserialize for concrete entity when validate and have information from assert in endpoint, but for DateTime this not work I use FosRestBundle and JMSSerializer this is my action

 /**
 * 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);
            }

Answers


Jason Roman February 2016

When you call deserialize(), that is already checking your entity for validity against your Doctrine annotations since the JMS Serializer already integrates with the Doctrine ORM.

If deserializing fails, an exception is thrown, which is what you are seeing.

All you need to do in your case is put your code in a try/catch block if you want to handle that yourself:

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);
    }
}

I didn't see your view() function but I assumed it was expecting an array of strings of error messages so I cast exception message to an array. Either way you get the idea.

Post Status

Asked in February 2016
Viewed 1,866 times
Voted 6
Answered 1 times

Search




Leave an answer