What is the best way to validate POST data to a Symfony2 Rest API using the FOSRestBundle? I would usually use route requirements, but with FOSRestBundle automatically generating the routes, I'm not sure the best way to go about it?
1 Answer
POST data can be validated that same way that GET data can be, if you're using the Param annotations. So for example, if you need an ID and password, you might use:
/**
* POST /login.json
*
* @RequestParam(name="id", requirements="\d+", description="User id")
* @RequestParam(name="password", requirements="\w{8,}", description="Password")
*/
public function postLoginAction(ParamFetcherInterface $paramFetcher)
{
...
}
Note the use of @RequestParam rather than @QueryParam - the former is for POST, the latter is for GET. Check out the example application if you need more examples on using the Param annotation.
1 Comment
chalasr
Right ! This answer should be accepted, it was exactly what I'm looking for, and my google search is exactly the title of your question.