Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hantsy opened this issue Nov 8, 2021 · 1 comment
Open

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

hantsy opened this issue Nov 8, 2021 · 1 comment

Comments

Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
@hantsy
Copy link

@hantsy hantsy commented Nov 8, 2021

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 throw an ContraintsVoilationException.

Additional Context

No response

@ro0NL
Copy link
Contributor

@ro0NL ro0NL commented Nov 8, 2021

IIUC this could pre-validate the Request object as well right? That be sweet.

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment