Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Router] Validate Request in Router #48908

Closed
@y4roc

Description

@y4roc

In the controller I use an attribute to which I pass as parameter the class of the RequestClass. A subscriber then loads the post content and validates it using the RequestClass. Subsequently, an instance of the RequestClass, with the values of the request, is passed as an argument to the controller method.

If the validation fails, the call throws a bad request exception.

Example:

class IndexAction {
  #[RequestValidator(class: IndexRequest::class)]
  public function __invoke(IndexRequest $request) {
    ...
  }
}
class IndexRequest {
  #[Assert\NotNull]
  #[Assert\Type(type: 'string')]
  public $username

  public function getUsername(): string {
    return $this->string;
  }
}

Originally posted by @y4roc in #46991 (comment)

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