Skip to content

Commit 3c61681

Browse files
Johan Montenijpascalbaljet
Johan Montenij
andauthored
Automatically add a red star to the label when a field is marked as required (#369)
Co-authored-by: Pascal Baljet <[email protected]>
1 parent 0bc146b commit 3c61681

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

config/splade.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,13 @@
6666
* @see https://splade.dev/docs/form-overview
6767
*/
6868
'blade' => [
69-
'component_prefix' => 'splade',
70-
'table_cell_directive' => 'cell',
71-
'escape_validation_messages' => true,
72-
'seo_title_directive' => 'seoTitle',
73-
'seo_description_directive' => 'seoDescription',
74-
'seo_keywords_directive' => 'seoKeywords',
69+
'component_prefix' => 'splade',
70+
'table_cell_directive' => 'cell',
71+
'asterisk_on_required_form_elements' => false,
72+
'escape_validation_messages' => true,
73+
'seo_title_directive' => 'seoTitle',
74+
'seo_description_directive' => 'seoDescription',
75+
'seo_keywords_directive' => 'seoKeywords',
7576
],
7677

7778
/**

resources/lang/nl.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
"Search": "Zoeken",
1717
"Select all on this page": "Selecteer alles op deze pagina",
1818
"Select all results": "Selecteer alle resultaten",
19+
"This field is required": "Dit veld is verplicht",
1920
"of": "van",
2021
"per page": "per pagina",
2122
"results": "resultaten",
2223
"to": "tot"
23-
}
24+
}

resources/views/form/label.blade.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
<span class="block mb-1 text-gray-700 font-sans">{{ $label }}</span>
1+
<span class="block mb-1 text-gray-700 font-sans">
2+
{!! $label !!}
3+
@if($attributes->has('required') || $attributes->has('data-required'))
4+
<span aria-hidden="true" class="text-red-600" title="{{ __('This field is required') }}">*</span>
5+
@endif
6+
</span>

src/FormBuilder/Component.php

+8
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ public function required(bool $required = true): self
158158
{
159159
if ($required) {
160160
$this->rules[] = 'required';
161+
162+
if (config('splade.blade.asterisk_on_required_form_elements', false)) {
163+
$this->attributes['data-required'] = true;
164+
}
161165
}
162166

163167
return $this;
@@ -191,6 +195,10 @@ public function rules(...$rules): self
191195
return explode('|', $item);
192196
})->flatten()->toArray();
193197

198+
if (config('splade.blade.asterisk_on_required_form_elements', false) && in_array('required', $this->rules)) {
199+
$this->attributes['data-required'] = true;
200+
}
201+
194202
return $this;
195203
}
196204

0 commit comments

Comments
 (0)