user3245821
user3245821

Reputation: 167

Symfony2 - Requirements for post parameters

I am relativeley new to Symfony and building a Rest API with the FriendsOfSymfony RestBundle.

My problem is that I want to use the requirements, which I used for my GET routes, for my POST routes. Is there any way to do so?

So practically something like this:

@Post("/article/{articleId}", requirements={"articleId"="\d+"})

Apparantly the ParamFetcher is also only for GET parameters although I couldn't find any specific information about this.

I'd be greatful for any help on this topic.

Upvotes: 2

Views: 1072

Answers (1)

ggioffreda
ggioffreda

Reputation: 650

The ParamFetcher works just fine with POST parameters as well. You can use the annotation:

use FOS\RestBundle\Controller\Annotations\RequestParam;

/**
 * @RequestParam(
 *   name="",
 *   key=null,
 *   requirements="",
 *   default=null,
 *   description="",
 *   strict=true,
 *   array=false,
 *   nullable=false
 * )
 */

Just remember to enable the related listeners otherwise it won't work:

fos_rest:
    param_fetcher_listener: true
    body_listener: true

Reference and documentation here: https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/3-listener-support.md

Upvotes: 1

Related Questions