Skip to content

[5.x] Fix unique entry value validation for date fields #11569

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: 5.x
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
53 changes: 46 additions & 7 deletions src/Rules/UniqueEntryValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Carbon;
use Statamic\Facades\Entry;

class UniqueEntryValue implements ValidationRule
Expand All @@ -28,13 +29,23 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
$query->where('site', $this->site);
}

$existing = $query
->when(
is_array($value),
fn ($query) => $query->whereIn($attribute, $value),
fn ($query) => $query->where($attribute, $value)
)
->first();
if ($this->isDateValue($value)) {
$existing = $query
->when(
is_array($value), // for mode: range
fn ($query) => $query->whereJsonContains($attribute, $this->convertDateValue($value)),
fn ($query) => $query->where($attribute, $this->convertDateValue($value))
)
->first();
} else {
$existing = $query
->when(
is_array($value),
fn ($query) => $query->whereIn($attribute, $value),
fn ($query) => $query->where($attribute, $value)
)
->first();
}

if (! $existing) {
return;
Expand All @@ -46,4 +57,32 @@ public function validate(string $attribute, mixed $value, Closure $fail): void

$fail('statamic::validation.unique_entry_value')->translate();
}

private function convertDateValue(Carbon|array $value): string|array
{
if (! is_array($value)) {
return $value->toDateString();
}

return collect($value)
->map(fn ($v) => $v instanceof Carbon ? $v->toDateString() : $v)
->toArray();
}

private function isDateValue($value): bool
{
if ($value instanceof Carbon) {
return true;
}

if (! is_array($value)) {
return false;
}

if (count($value) == 2 && isset($value['start']) && isset($value['end'])) {
return true;
}

return false;
}
}
26 changes: 26 additions & 0 deletions tests/Validation/UniqueEntryValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,30 @@ public function it_passes_when_theres_a_duplicate_entry_value_in_a_different_sit
['slug' => new UniqueEntryValue(collection: 'collection-one', site: 'site-two')]
)->passes());
}

#[Test]
public function it_fails_when_theres_a_duplicate_date_value()
{
EntryFactory::id('123')->slug('foo')->collection('collection-one')
->data(['test_date' => '2021-11-15 20:31:04'])
->create();

$this->assertTrue(Validator::make(
['test_date' => '2021-11-15 20:31:04'],
['test_date' => new UniqueEntryValue]
)->fails());
}

#[Test]
public function it_fails_when_theres_a_duplicate_date_range_value()
{
EntryFactory::id('123')->slug('foo')->collection('collection-one')
->data(['test_date' => ['start' => '2021-11-14 09:00:00', 'end' => '2021-11-15 20:31:04']])
->create();

$this->assertTrue(Validator::make(
['test_date' => ['start' => '2021-11-14 09:00:00', 'end' => '2021-11-15 20:31:04']],
['test_date' => new UniqueEntryValue]
)->fails());
}
}