Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 45 additions & 0 deletions tests/Tags/Form/FormCreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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([
'<label><input id="[[form-handle]]-form-favourite-animals-field-cat-option" type="checkbox" name="favourite_animals[]" value="cat">Cat</label>',
'<br>',
'<label><input id="[[form-handle]]-form-favourite-animals-field-armadillo-option" type="checkbox" name="favourite_animals[]" value="armadillo">Armadillo</label>',
], [
'handle' => 'favourite_animals',
'field' => [
'type' => 'checkboxes',
'validate' => ['required'],
'options' => [
'cat' => 'Cat',
'armadillo' => 'Armadillo',
],
],
]);

$this->assertFieldRendersHtml([
'<label><input id="[[form-handle]]-form-terms-field-yes-option" type="checkbox" name="terms[]" value="yes" required>I agree</label>',
], [
'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()
{
Expand Down
Loading