From c92c7e75ad962ff0bc816cad5cd3bdebe78f1fed Mon Sep 17 00:00:00 2001 From: Lazizbek Ergashev Date: Sun, 12 Jul 2026 18:12:18 +0500 Subject: [PATCH 1/2] Fix link fieldtype ignoring sometimes validator --- src/Fieldtypes/Link.php | 2 +- tests/Fieldtypes/LinkTest.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Fieldtypes/Link.php b/src/Fieldtypes/Link.php index d5ca4200819..dd4d38c55ee 100644 --- a/src/Fieldtypes/Link.php +++ b/src/Fieldtypes/Link.php @@ -146,7 +146,7 @@ public function preload() private function initialOption($value, $entry, $asset) { if (! $value) { - $fallback = $this->field->isRequired() ? 'url' : null; + $fallback = $this->field->isRequired() && ! collect($this->field->rules()[$this->field->handle()])->contains('sometimes') ? 'url' : null; $option = $this->field->get('default_option', $fallback); if ($option === 'first-child' && ! $this->showFirstChildOption()) { diff --git a/tests/Fieldtypes/LinkTest.php b/tests/Fieldtypes/LinkTest.php index bdab29b236d..d194514389f 100644 --- a/tests/Fieldtypes/LinkTest.php +++ b/tests/Fieldtypes/LinkTest.php @@ -295,6 +295,7 @@ public static function initialOptionProvider(): array 'asset falls back to url when unavailable and required' => [['type' => 'link', 'default_option' => 'asset', 'required' => true], null, false, 'url'], 'asset falls back to null when unavailable and optional' => [['type' => 'link', 'default_option' => 'asset'], null, false, null], 'existing value overrides default option' => [['type' => 'link', 'default_option' => 'entry'], 'https://example.com', false, 'url'], + 'null when has sometimes rule even if required' => [['type' => 'link', 'required' => true, 'validate' => 'sometimes'], null, false, null], ]; } } From 857c1077de630fce284cd8bf6f2724eb530eef91 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 13 Jul 2026 17:08:42 -0400 Subject: [PATCH 2/2] Reuse Field::hasSometimesRule() in Link fieldtype instead of duplicating rule check Co-Authored-By: Claude Sonnet 5 --- src/Fields/Field.php | 2 +- src/Fieldtypes/Link.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 2af853df882..9c72f715fd6 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -181,7 +181,7 @@ public function isRequired() return collect($this->rules()[$this->handle])->contains('required'); } - private function hasSometimesRule() + public function hasSometimesRule() { return collect($this->rules()[$this->handle])->contains('sometimes'); } diff --git a/src/Fieldtypes/Link.php b/src/Fieldtypes/Link.php index dd4d38c55ee..3bacea22de8 100644 --- a/src/Fieldtypes/Link.php +++ b/src/Fieldtypes/Link.php @@ -146,7 +146,7 @@ public function preload() private function initialOption($value, $entry, $asset) { if (! $value) { - $fallback = $this->field->isRequired() && ! collect($this->field->rules()[$this->field->handle()])->contains('sometimes') ? 'url' : null; + $fallback = $this->field->isRequired() && ! $this->field->hasSometimesRule() ? 'url' : null; $option = $this->field->get('default_option', $fallback); if ($option === 'first-child' && ! $this->showFirstChildOption()) {