Skip to content

Commit

Permalink
Add config for values validation
Browse files Browse the repository at this point in the history
  • Loading branch information
afonic committed Jan 25, 2024
1 parent 365ecdd commit fa19ed0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
3 changes: 3 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,

];
4 changes: 3 additions & 1 deletion src/Http/Livewire/LfCheckboxFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/Http/Livewire/LfRadioFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 20 additions & 1 deletion tests/Feature/LfCheckboxFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down
21 changes: 20 additions & 1 deletion tests/Feature/LfRadioFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit fa19ed0

Please sign in to comment.