1

I have an API which takes multiple input values. One of them is a date. When the date is sent, all is fine. But, when the user isn't sending a date, I have an error 500 with this error message:

Invalid datetime "Some invalid data", expected format Y-m-d\TH:i:sP.

So I wanted to check if the data sent had the format required.

But I don't understand how things are working, hope you can help.

This is what I have

/**
 *
 * @Rest\Post(
 *     path = "/signup",
 *     name = "api_users_add"
 * )
 * @Rest\View(StatusCode=201, serializerGroups={"user_detail"})
 * @ParamConverter(
 *     "user",
 *     converter="fos_rest.request_body",
 *     options={"deserializationContent"={"groups"={"Deserialize"}}},
 * )
 * @ParamConverter(
 *     "profile",
 *     converter="fos_rest.request_body",
 *     options={"deserializationContent"={"groups"={"Deserialize"}}},
 * )
 */
public function postUserAction(Request $request, User $user, Profile $profile)
{
    if (!preg_match("^[0-9]{4}-[0-1][0-9]-[0-3][0-9]$",$profile->getBirth())){
        return new JsonResponse([
            'success' => false,
            'message' => "Date d'anniversaire au mauvais format"
        ]);
    }
}

But in fact, I never go into this condition, the error 500 is triggered before.

I guess this has something to do with the @ParamConverter from Profile who can't "deserialize" when birth is not a DateTime.

Thing is, I would like to check what is sent (as I did into my condition) in order to avoid this internal error. But I can't find where this is handled on my code.

Thanks for the help.

Considering this : symfony doc for Param converter fos rest bundle

What I am looking for is to find where the validationErrors are specified.

Bilaal Rashid
  • 828
  • 2
  • 13
  • 21
Minirock
  • 628
  • 3
  • 13
  • 28
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Dave Feb 27 '19 at 14:27
  • @Dave What this has to do with my issue ? I might be dumb or blind but I don't see. – Minirock Feb 27 '19 at 14:33
  • Look through the list of errors for 500 and you will find details on where to look to find out what is really happening (hint, it's not likely related to what you think). – Dave Feb 27 '19 at 14:37
  • I have already been looking my dev.log...I know what id the error: Invalid datetime "YY--dd-mm", expected format Y-m-d\TH:i:sP. – Minirock Feb 27 '19 at 14:43
  • That's important information that should be in your question. Please edit it and include that data. Your API that is expecting a date appears to not be checking to make sure it got a date in the correct format and is trying to manipulate it without knowing that it is valid/correct. – Dave Feb 27 '19 at 14:52
  • @Dave Exactly, I might not be clear, but this is what I already say in my question ==> So I wanted to check if the data sent had the format required. – Minirock Feb 27 '19 at 14:54
  • Find where `postUserAction` is called and right before it add `var_dump($profile->getBirth();` which should show you the date value. It isn't clear from what you've given us where the code you've posted comes into play. – Dave Feb 27 '19 at 15:06
  • @Dave it's an api rest...that means it will be called each time we are sending a post request to the url `/signup` I'm never calling this method directly. The post params from the request are converted into a Profile entity. And this is what I look for, where the conversion of the params are made into the User entity. – Minirock Feb 27 '19 at 15:27

0 Answers0