Hakim
Hakim

Reputation: 1102

Symfony 2 Validate route params with FOSRestBundle

I have a delete route like so, using FOSRestBundle

/**
 * @Route("/delete/{id}")
 * @Security("has_role('ROLE_ADMIN')")
 * @Rest\View
 */
public function deleteAction(Request $request, $id)
{
    ...
}

I want to make sure that the id parameter is numeric. I tried :

@Route("/delete/{id}", requirements={"id" = "\d+"})

And

@QueryParam(name="id", requirements="\d+", description="User id")

And

@RequestParam(name="id", requirements="\d+", description="User id")

But none of these solutions work. Either the route is not found, or the constraint is not respected.

Upvotes: 0

Views: 777

Answers (1)

Hakim
Hakim

Reputation: 1102

This works, but does not return a JSON encoded 404 response when trying to access a route like api/users/xx.json

@Rest\Delete("/{id}", requirements={"id" = "\d+"}, defaults={"id" = 1})

Upvotes: 2

Related Questions