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

[Validator] Attribute can not be applied on Controller method argument #43958

Copy link
Copy link
Closed
@hantsy

Description

@hantsy
Issue body actions

Symfony version(s) affected

5.3.x

Description

Java Bean validation can be applied on method argument in a MVC Controller or other Jakarta EE components, but this does not work in Symfony.

I got the following exception when adding attributes on method arguments of a Controller.

 Uncaught Error: Attribute "Symfony\Component\Validator\Constraints\PositiveOrZero" cannot target parameter (allowed targets: method, prop
erty)

How to reproduce

Create a method like this.

  #[Route(path: "", name: "all", methods: ["GET"])]
  function all(string $keyword, #[PositiveOrZero] int $offset = 0, #[Positive] int $limit = 20): Response
  {
      $data = $this->posts->findByKeyword($keyword||'', $offset, $limit);
      return $this->json($data);
  }

Possible Solution

Make it work, and if validation failed throws an ContraintsVoilationException.

Additional Context

Add a built-in EventListener to handle this exception by default.

Metadata

Metadata

Assignees

No one assigned

    Labels

    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.