Skip to content

Commit 85fd9d5

Browse files
authored
Merge pull request #3242 from yajra/min-search-length
feat: add min search length control
2 parents 12addca + 7104ed7 commit 85fd9d5

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

src/CollectionDataTable.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ public function paging(): void
143143
public function make(bool $mDataSupport = true): JsonResponse
144144
{
145145
try {
146+
$this->validateMinLengthSearch();
147+
146148
$this->totalRecords = $this->totalCount();
147149

148150
if ($this->totalRecords) {

src/DataTableAbstract.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ abstract class DataTableAbstract implements DataTable
122122

123123
protected bool $editOnlySelectedColumns = false;
124124

125+
protected int $minSearchLength = 0;
126+
125127
/**
126128
* Can the DataTable engine be created with these parameters.
127129
*
@@ -989,4 +991,26 @@ protected function getPrimaryKeyName(): string
989991
{
990992
return 'id';
991993
}
994+
995+
public function minSearchLength(int $length): static
996+
{
997+
$this->minSearchLength = $length;
998+
999+
return $this;
1000+
}
1001+
1002+
protected function validateMinLengthSearch(): void
1003+
{
1004+
if ($this->request->isSearchable()
1005+
&& $this->minSearchLength > 0
1006+
&& Str::length($this->request->keyword()) < $this->minSearchLength
1007+
) {
1008+
$this->totalRecords = 0;
1009+
$this->filteredRecords = 0;
1010+
throw new \Exception(
1011+
__('Please enter at least :length characters to search.', ['length' => $this->minSearchLength]),
1012+
400
1013+
);
1014+
}
1015+
}
9921016
}

src/QueryDataTable.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ public static function canCreate($source): bool
124124
public function make(bool $mDataSupport = true): JsonResponse
125125
{
126126
try {
127+
$this->validateMinLengthSearch();
128+
127129
$results = $this->prepareQuery()->results();
128130
$processed = $this->processResults($results, $mDataSupport);
129131
$data = $this->transform($results, $processed);
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Tests\Integration;
4+
5+
use Illuminate\Foundation\Testing\DatabaseTransactions;
6+
use Illuminate\Support\Facades\Route;
7+
use PHPUnit\Framework\Attributes\Test;
8+
use Yajra\DataTables\EloquentDataTable;
9+
use Yajra\DataTables\Tests\Models\User;
10+
use Yajra\DataTables\Tests\TestCase;
11+
12+
class MinSearchLengthDataTableTest extends TestCase
13+
{
14+
use DatabaseTransactions;
15+
16+
#[Test]
17+
public function it_returns_all_records_when_search_is_empty()
18+
{
19+
$crawler = $this->call('GET', '/eloquent/min-length', [
20+
'start' => 0,
21+
'length' => 10,
22+
'columns' => [
23+
['data' => 'id'],
24+
['data' => 'name'],
25+
['data' => 'email'],
26+
],
27+
'search' => [
28+
'value' => '',
29+
'regex' => false,
30+
],
31+
]);
32+
33+
$crawler->assertJson([
34+
'draw' => 0,
35+
'recordsTotal' => 20,
36+
'recordsFiltered' => 20,
37+
]);
38+
}
39+
40+
#[Test]
41+
public function it_returns_an_error_when_search_keyword_length_is_less_than_required()
42+
{
43+
$crawler = $this->call('GET', '/eloquent/min-length', [
44+
'start' => 0,
45+
'length' => 10,
46+
'columns' => [
47+
['data' => 'id'],
48+
['data' => 'name'],
49+
['data' => 'email'],
50+
],
51+
'search' => [
52+
'value' => 'abc',
53+
'regex' => false,
54+
],
55+
]);
56+
57+
$crawler->assertJson([
58+
'draw' => 0,
59+
'recordsTotal' => 0,
60+
'recordsFiltered' => 0,
61+
'data' => [],
62+
'error' => "Exception Message:\n\nPlease enter at least 5 characters to search.",
63+
]);
64+
}
65+
66+
#[Test]
67+
public function it_returns_filtered_records_when_search_keyword_length_is_met()
68+
{
69+
$crawler = $this->call('GET', '/eloquent/min-length', [
70+
'draw' => 1,
71+
'start' => 0,
72+
'length' => 10,
73+
'columns' => [
74+
['data' => 'id'],
75+
['data' => 'name'],
76+
['data' => 'email'],
77+
],
78+
'search' => [
79+
'value' => 'Record-17',
80+
'regex' => false,
81+
],
82+
]);
83+
84+
$crawler->assertJson([
85+
'draw' => 1,
86+
'recordsTotal' => 20,
87+
'recordsFiltered' => 1,
88+
]);
89+
}
90+
91+
protected function setUp(): void
92+
{
93+
parent::setUp();
94+
95+
Route::get('/eloquent/min-length', fn () => (new EloquentDataTable(User::query()))
96+
->minSearchLength(5)
97+
->toJson());
98+
}
99+
}

0 commit comments

Comments
 (0)