1

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 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.