Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[HTTPFoundation] Get param or throw BadRequestHttpException #47575

Copy link
Copy link
Closed
@raziel057

Description

@raziel057
Issue body actions

Description

Hello,

What do you think about providing helpers to get request or query parameter value using the request object with same behavior as what can be done using the requirements in routes (for query params). If the value doesn't matches the requirements, the method throw a BadRequestException.

The goal would be to avoid boilerplate like this:

$formDefinitionId = $request->request->get('formDefId');

if (preg_match('/\d+/', $formDefinitionId)) {
    $formDefinitionId = (int) $formDefinitionId;
} else {
    throw new BadRequestHttpException('Invalid request parameter: "formDefId"');
}

It would be nice to call a method like this:

$formDefinitionId = (int) $request->request->getForRequirement('formDefId', '\d+');

In that case if the parameter value does not match the requirement it automatically throw a BadRequestHttpException.

This is a small thing but I always encounter projects that does not manage correctly/safely the request parameters due to the lack of such helper. In most case I find the following call:

$formDefinitionId = $request->request->getInt('formDefId');

Rather than returning a BadRequest, the process is done with $formDefinitionId = 0 which can lead to unexpected queries etc.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.