Skip to content

feat: add min search length control #3242

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

Open
wants to merge 4 commits 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: 2 additions & 0 deletions src/CollectionDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ public function paging(): void
public function make(bool $mDataSupport = true): JsonResponse
{
try {
$this->validateMinLengthSearch();

$this->totalRecords = $this->totalCount();

if ($this->totalRecords) {
Expand Down
24 changes: 24 additions & 0 deletions src/DataTableAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ abstract class DataTableAbstract implements DataTable

protected bool $editOnlySelectedColumns = false;

protected int $minSearchLength = 0;

/**
* Can the DataTable engine be created with these parameters.
*
Expand Down Expand Up @@ -989,4 +991,26 @@ protected function getPrimaryKeyName(): string
{
return 'id';
}

public function minSearchLength(int $length): static
{
$this->minSearchLength = $length;

return $this;
}

protected function validateMinLengthSearch(): void
{
if ($this->request->isSearchable()
&& $this->minSearchLength > 0
&& Str::length($this->request->keyword()) < $this->minSearchLength
) {
$this->totalRecords = 0;
$this->filteredRecords = 0;
throw new \Exception(
__('Please enter at least :length characters to search.', ['length' => $this->minSearchLength]),
400
);
}
}
}
2 changes: 2 additions & 0 deletions src/QueryDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ public static function canCreate($source): bool
public function make(bool $mDataSupport = true): JsonResponse
{
try {
$this->validateMinLengthSearch();

$results = $this->prepareQuery()->results();
$processed = $this->processResults($results, $mDataSupport);
$data = $this->transform($results, $processed);
Expand Down
99 changes: 99 additions & 0 deletions tests/Integration/MinSearchLengthDataTableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Yajra\DataTables\Tests\Integration;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Route;
use PHPUnit\Framework\Attributes\Test;
use Yajra\DataTables\EloquentDataTable;
use Yajra\DataTables\Tests\Models\User;
use Yajra\DataTables\Tests\TestCase;

class MinSearchLengthDataTableTest extends TestCase
{
use DatabaseTransactions;

#[Test]
public function it_returns_all_records_when_search_is_empty()
{
$crawler = $this->call('GET', '/eloquent/min-length', [
'start' => 0,
'length' => 10,
'columns' => [
['data' => 'id'],
['data' => 'name'],
['data' => 'email'],
],
'search' => [
'value' => '',
'regex' => false,
],
]);

$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 20,
'recordsFiltered' => 20,
]);
}

#[Test]
public function it_returns_an_error_when_search_keyword_length_is_less_than_required()
{
$crawler = $this->call('GET', '/eloquent/min-length', [
'start' => 0,
'length' => 10,
'columns' => [
['data' => 'id'],
['data' => 'name'],
['data' => 'email'],
],
'search' => [
'value' => 'abc',
'regex' => false,
],
]);

$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 0,
'recordsFiltered' => 0,
'data' => [],
'error' => "Exception Message:\n\nPlease enter at least 5 characters to search.",
]);
}

#[Test]
public function it_returns_filtered_records_when_search_keyword_length_is_met()
{
$crawler = $this->call('GET', '/eloquent/min-length', [
'draw' => 1,
'start' => 0,
'length' => 10,
'columns' => [
['data' => 'id'],
['data' => 'name'],
['data' => 'email'],
],
'search' => [
'value' => 'Record-17',
'regex' => false,
],
]);

$crawler->assertJson([
'draw' => 1,
'recordsTotal' => 20,
'recordsFiltered' => 1,
]);
}

protected function setUp(): void
{
parent::setUp();

Route::get('/eloquent/min-length', fn () => (new EloquentDataTable(User::query()))
->minSearchLength(5)
->toJson());
}
}