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 d5ca4200819..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() ? 'url' : null; + $fallback = $this->field->isRequired() && ! $this->field->hasSometimesRule() ? '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], ]; } }