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

Set maximum records limit per-page in config. #3171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/QueryDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ public function paging(): void
$start = $this->request->start();
$length = $this->request->length();

$limit = $length > 0 ? $length : 10;
$limit = $length > 0 ? min($length, $this->config->maxLimit()) : 10;

if (is_callable($this->limitCallback)) {
$this->query->limit($limit);
Expand Down
10 changes: 10 additions & 0 deletions src/Utilities/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,14 @@
{
return (array) $this->repository->get('datatables.json.header', []);
}

/**
* Get the maximum record limit for one page.
*
* @return int The maximum limit value.
*/
public function maxLimit(): int
{
return (int) $this->repository->get('datatables.max_limit', 100);

Check failure on line 104 in src/Utilities/Config.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, prefer-stable)

Cannot cast mixed to int.

Check failure on line 104 in src/Utilities/Config.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3, prefer-stable)

Cannot cast mixed to int.
}
}
6 changes: 6 additions & 0 deletions src/config/datatables.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,10 @@
* Callbacks needs to start by those terms, or they will be cast to string.
*/
'callback' => ['$', '$.', 'function'],

/*
* Maximum record limit per-page. To prevent user from arbitrarily request large record set.
* If 'limit' query parameter in the request exceed this limit, it will be automatically set to this value.
*/
'max_limit' => 100,
];
Loading