diff --git a/resources/views/extend/forms/antlers/fields/checkboxes.antlers.html b/resources/views/extend/forms/antlers/fields/checkboxes.antlers.html index 87846383d31..60fdd90813a 100644 --- a/resources/views/extend/forms/antlers/fields/checkboxes.antlers.html +++ b/resources/views/extend/forms/antlers/fields/checkboxes.antlers.html @@ -8,7 +8,7 @@ value="{{ option }}" {{ if js_driver }}{{ js_attributes }}{{ /if }} {{ if value|in_array:option }}checked{{ /if }} - {{ if validate|contains:required }}required{{ /if }} + {{ if validate|contains:required && options|count == 1 }}required{{ /if }} {{ if error }} aria-invalid="true" aria-describedby="{{ id }}-error" {{ elseif instructions }} diff --git a/resources/views/extend/forms/blade/fields/checkboxes.blade.php b/resources/views/extend/forms/blade/fields/checkboxes.blade.php index ce24cf3cd60..911bdc7e75b 100644 --- a/resources/views/extend/forms/blade/fields/checkboxes.blade.php +++ b/resources/views/extend/forms/blade/fields/checkboxes.blade.php @@ -12,7 +12,7 @@ value="{{ $option }}" @if (isset($js_driver)) {!! $js_attributes !!} @endif @checked(in_array($option, $value ?? [])) - @required(in_array('required', $validate ?? [])) + @required(in_array('required', $validate ?? []) && count($options) === 1) @if ($error) aria-invalid="true" aria-describedby="{{ $id }}-error" @elseif ($instructions) diff --git a/tests/Tags/Form/FormCreateTest.php b/tests/Tags/Form/FormCreateTest.php index 113fd0c90b8..43d196509dc 100644 --- a/tests/Tags/Form/FormCreateTest.php +++ b/tests/Tags/Form/FormCreateTest.php @@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Storage; use Illuminate\Validation\ValidationException; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; use Statamic\Facades\AssetContainer; use Statamic\Facades\Form; @@ -502,6 +503,50 @@ public function it_dynamically_renders_inline_checkboxes_field() ]); } + #[Test] + #[DataProvider('templateLanguageProvider')] + public function it_only_renders_required_attribute_on_checkboxes_field_with_a_single_option($language) + { + config(['statamic.templates.language' => $language]); + + $this->assertFieldRendersHtml([ + '', + '
', + '', + ], [ + 'handle' => 'favourite_animals', + 'field' => [ + 'type' => 'checkboxes', + 'validate' => ['required'], + 'options' => [ + 'cat' => 'Cat', + 'armadillo' => 'Armadillo', + ], + ], + ]); + + $this->assertFieldRendersHtml([ + '', + ], [ + 'handle' => 'terms', + 'field' => [ + 'type' => 'checkboxes', + 'validate' => ['required'], + 'options' => [ + 'yes' => 'I agree', + ], + ], + ]); + } + + public static function templateLanguageProvider() + { + return [ + 'antlers' => ['antlers'], + 'blade' => ['blade'], + ]; + } + #[Test] public function it_dynamically_renders_radio_field() {