The offset value must be greater than or equal to zero – CodeIgniter 4 Pagination library
0 reputation · 29 Feb 2020, 00:54 UTC
0 reputation · 29 Feb 2020, 00:54 UTC
The goal is to use CodeIgniter 4’s Pagination library to display large datasets without encountering the validation error "The offset value must be greater than or equal to zero." The library computes the offset as ($page - 1) * $perPage and passes it directly to the Query Builder’s limit method, which throws the error when the offset is negative. There is no automatic guard against negative offsets in the pagination component, leaving it to developers to ensure that page numbers and per_page settings produce only non‑negative values.
This raises uncertainty about whether the framework should clamp negative offsets to zero, validate page numbers internally, or rely on developer‑side checks. The lack of clear guidance in the documentation makes the behavior ambiguous across different deployment scenarios.
35275 reputation · 29 Feb 2020, 09:05 UTC
($page - 1) * $perPage, so a page value below 1 produces a negative number before the Query Builder ever sees it.page >= 1 on the server side is acceptable and is the developer’s responsibility. The framework does not enforce this uniformly across CI4 releases.QueryBuilder::limit() should continue throwing DatabaseException for negative offsets. Silently coercing to zero would mask invalid input and change result sets for requests such as ?page=0.QueryBuilder::limit($limit, $offset) throws a DatabaseException with “The offset value must be greater than or equal to zero” when $offset < 0. The message originates from the database layer, not the pagination library.page is 0, negative, missing, or non-numeric, making ($page - 1) * $perPage negative. A per_page value of zero or less can also produce invalid pagination math.model->paginate() signatures vary across CodeIgniter 4 releases. The installed version determines which API applies.page and per_page from the request.page to at least 1 and per_page to at least 1 before computing any offset.max(0, ($page - 1) * $per_page) and pass that non-negative offset to QueryBuilder::limit().?page=0, ?page=-1, and a URL with no page parameter; each should return the first page without a DatabaseException.?page=1 and ?page=2; confirm offset is 0 for page 1 and equals per_page for page 2.getCompiledSelect() or the database debugger if available.Which CodeIgniter 4 version are you running, and are you calling model->paginate() or QueryBuilder::limit() directly? This changes the exact class names and method signatures to use.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.