From fa19ed087b7dae896f8be0c8cfd0f01381f0b630 Mon Sep 17 00:00:00 2001 From: Iosif Chatzimichail Date: Thu, 25 Jan 2024 10:40:23 +0200 Subject: [PATCH] Add config for values validation --- config/config.php | 3 +++ src/Http/Livewire/LfCheckboxFilter.php | 4 +++- src/Http/Livewire/LfRadioFilter.php | 4 +++- tests/Feature/LfCheckboxFilterTest.php | 21 ++++++++++++++++++++- tests/Feature/LfRadioFilterTest.php | 21 ++++++++++++++++++++- 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/config/config.php b/config/config.php index 21ac1f1..07e88af 100644 --- a/config/config.php +++ b/config/config.php @@ -7,4 +7,7 @@ // Only allow filters for fields and conditions already loaded in the page to be applied 'only_allow_active_filters' => true, + // Validate that the values of radio and checkbox filters are in the available options array + 'validate_filter_values' => true, + ]; diff --git a/src/Http/Livewire/LfCheckboxFilter.php b/src/Http/Livewire/LfCheckboxFilter.php index 3602e8f..5269a5c 100644 --- a/src/Http/Livewire/LfCheckboxFilter.php +++ b/src/Http/Livewire/LfCheckboxFilter.php @@ -31,7 +31,9 @@ public function filterOptions() public function updatedSelected() { - $this->validate(); + if (config('statamic-livewire-filters.validate_filter_values')) { + $this->validate(); + } $optionsToAdd = array_diff($this->selected, $this->previousSelected); $optionsToRemove = array_diff($this->previousSelected, $this->selected); diff --git a/src/Http/Livewire/LfRadioFilter.php b/src/Http/Livewire/LfRadioFilter.php index 4783123..d231bca 100644 --- a/src/Http/Livewire/LfRadioFilter.php +++ b/src/Http/Livewire/LfRadioFilter.php @@ -29,7 +29,9 @@ public function filterOptions() public function updatedSelected() { - $this->validate(); + if (config('statamic-livewire-filters.validate_filter_values')) { + $this->validate(); + } $this->dispatch('filter-updated', field: $this->field, diff --git a/tests/Feature/LfCheckboxFilterTest.php b/tests/Feature/LfCheckboxFilterTest.php index 3ac8eaa..3b58775 100644 --- a/tests/Feature/LfCheckboxFilterTest.php +++ b/tests/Feature/LfCheckboxFilterTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature; use Facades\Reach\StatamicLivewireFilters\Tests\Factories\EntryFactory; +use Illuminate\Support\Facades\Config; use Livewire\Livewire; use Reach\StatamicLivewireFilters\Http\Livewire\LfCheckboxFilter; use Reach\StatamicLivewireFilters\Tests\PreventSavingStacheItemsToDisk; @@ -146,7 +147,25 @@ public function it_does_not_accept_a_value_not_in_the_options_array() Livewire::test(LfCheckboxFilter::class, ['field' => 'item_options', 'collection' => 'pages', 'blueprint' => 'pages.pages', 'condition' => 'is']) ->assertSet('selected', []) ->set('selected', ['not-an-option']) - ->assertHasErrors('selected'); + ->assertHasErrors('selected') + ->assertNotDispatched('filter-updated'); + } + + /** @test */ + public function it_can_turn_off_validation_of_values_in_the_config() + { + Config::set('statamic-livewire-filters.validate_filter_values', false); + + Livewire::test(LfCheckboxFilter::class, ['field' => 'item_options', 'collection' => 'pages', 'blueprint' => 'pages.pages', 'condition' => 'is']) + ->assertSet('selected', []) + ->set('selected', ['not-an-option']) + ->assertSet('selected', ['not-an-option']) + ->assertDispatched('filter-updated', + field: 'item_options', + condition: 'is', + payload: 'not-an-option', + command: 'add', + ); } /** @test */ diff --git a/tests/Feature/LfRadioFilterTest.php b/tests/Feature/LfRadioFilterTest.php index b447f3c..3d99c0b 100644 --- a/tests/Feature/LfRadioFilterTest.php +++ b/tests/Feature/LfRadioFilterTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature; use Facades\Reach\StatamicLivewireFilters\Tests\Factories\EntryFactory; +use Illuminate\Support\Facades\Config; use Livewire\Livewire; use Reach\StatamicLivewireFilters\Http\Livewire\LfRadioFilter; use Reach\StatamicLivewireFilters\Tests\PreventSavingStacheItemsToDisk; @@ -112,7 +113,25 @@ public function it_does_not_accept_a_value_not_in_the_options_array() Livewire::test(LfRadioFilter::class, ['field' => 'item_options', 'collection' => 'pages', 'blueprint' => 'pages.pages', 'condition' => 'is']) ->assertSet('selected', '') ->set('selected', 'not-an-option') - ->assertHasErrors('selected'); + ->assertHasErrors('selected') + ->assertNotDispatched('filter-updated'); + } + + /** @test */ + public function it_can_turn_off_validation_in_the_config() + { + Config::set('statamic-livewire-filters.validate_filter_values', false); + + Livewire::test(LfRadioFilter::class, ['field' => 'item_options', 'collection' => 'pages', 'blueprint' => 'pages.pages', 'condition' => 'is']) + ->assertSet('selected', '') + ->set('selected', 'not-an-option') + ->assertSet('selected', 'not-an-option') + ->assertDispatched('filter-updated', + field: 'item_options', + condition: 'is', + payload: 'not-an-option', + command: 'replace', + ); } /** @test */