greg
greg

Reputation: 6913

Validating data with Symfony2 FOSRestBundle

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?

Upvotes: 1

Views: 3149

Answers (1)

Andrew Stirling
Andrew Stirling

Reputation: 285

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.

Upvotes: 3

Related Questions