0

In Symfony 3.4, is there a way to validate optional input GET parameters passed in through the querystring as integers?

If provided, I am using the $ownerId and $courseId to query the corresponding repositories, however the value needs to be an integer otherwise the query falls over.

This is what I have so far and it matches the docs, but it doesn't seem to force any validation or graceful handling of passing through http://www.crmpicco.co.uk/book-teeoff/belleisle/ayrshire/?ownerid=crmpicco&courseId=rfc1872, for example.

/**
 * @Route(
 *      "/book-teeoff/{course}/{area}",
 *      name = "book_teeoff",
 *      requirements={"ownerId"="\d+","courseId"="\d+"},
 *      methods={"GET"}
 * )
 *
 * @param Request $request
 *
 * @return Response
 */
public function bookTeeoffAction(Request $request): Response
{
    // these are *optional*, but if provided need to be integers
    $ownerId = $request->get('ownerId');
    $courseId = $request->get('courseId');
crmpicco
  • 16,605
  • 26
  • 134
  • 210
  • 1
    You can use more specific getters, e.g. `$request->query->getInt('ownerId')`. Does that solve your issue? See: https://github.com/symfony/symfony/blob/3.4/src/Symfony/Component/HttpFoundation/ParameterBag.php#L160-L171 – dbrumann Jan 30 '20 at 11:06
  • The requirements definition is for placeholder of your route (`course` and `area` here). Maybe define a specific route when `ownerId` and/or `courseId` is provided is clearer ? You could fine a solution with [param converter](https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#creating-a-converter), but it sound a bit hacky to run validation here. – Mcsky Jan 30 '20 at 12:54

0 Answers0