Конечная точка моего контроллера:
Код: Выделить всё
#[SuccessfulPaginatedResponse(description: 'Returns paginated list of categories by given filters.')]
#[ValidationErrorResponse]
#[Route(name: 'categories_index', methods: ['GET'], format: 'json')]
public function index(
#[MapQueryString(
validationGroups: ['default'],
validationFailedStatusCode: Response::HTTP_UNPROCESSABLE_ENTITY
)] IndexRequestQueryDto $queryParams,
Request $request
): JSONResponse {
$filter = CategoryFilterFactory::createFromIndexRequest($queryParams);
$result = $this->categoryService->fetch($filter);
$paginatedResponse = PaginatedResponse::create(
$result['data'],
$queryParams,
$result['totalCount'],
$request
);
return $this->json($paginatedResponse->toArray());
}
Код: Выделить всё
class IndexRequestQueryDto extends PaginatedRequest
{
public function __construct(
#[Assert\Positive]
#[Assert\Type('integer')]
public readonly ?int $businessUnit = null,
#[Assert\Positive]
#[Assert\Type('integer')]
public readonly ?int $parentId = null,
#[Assert\Type('string')]
public readonly ?string $name = null,
?int $page = null,
?int $pageSize = null,
) {
parent::__construct($page, $pageSize);
}
}
Код: Выделить всё
class PaginatedRequest implements DtoInterface
{
public const DEFAULT_PAGE = 1;
public const DEFAULT_PAGE_SIZE = 25;
public const MAX_PAGE_SIZE = 100;
public function __construct(
#[Assert\Type('integer')]
#[Assert\Positive]
public readonly ?int $page = null,
#[Assert\Type('integer')]
#[Assert\Positive]
#[Assert\LessThanOrEqual(self::MAX_PAGE_SIZE)]
public readonly ?int $pageSize = null,
) {
}
- Удалены все утверждения
- В IndexRequestQueryDto пытался определить страницу и размер страницы, не передавая их родительскому конструктору
- Пробовал использовать #[Assert\When()]
Единственное, что мне помогло, это использовать ValidatorInterface в моем контроллере, но это решение требует дополнительного кода и мне это не нравится:
Код: Выделить всё
$queryParams = new IndexRequestQueryDto(
$request->query->has('businessUnit') ? (int) $request->query->get('businessUnit') : null,
$request->query->has('parentId') ? (int) $request->query->get('parentId') : null,
$request->query->has('name') ? (string) $request->query->get('name') : null,
$request->query->has('page') ? (int) $request->query->get('page') : null,
$request->query->has('pageSize') ? (int) $request->query->get('pageSize') : null,
);
$errors = $this->validator->validate($queryParams);
if (count($errors) > 0) {
return $this->json(['errors' => $errors], Response::HTTP_UNPROCESSABLE_ENTITY);
}
Подробнее здесь: https://stackoverflow.com/questions/798 ... parameters
Мобильная версия