Boolean Checkbox for Blade with Bootstrap
Checkboxes are often tricky, because "unchecked" they wont be included inside the request.
Luckily this is easy circumvented with a hidden input with the same name and a (default) negative value.
Consider the following blade snippet:
<div class="form-group row">
<div class="col-sm-2 text-right">{{ $left_title }}</div>
<div class="col-sm-10">
<div class="form-check">
<input type="hidden" name="{{ $name }}" value="0">
<input class="form-check-input"
type="checkbox"
name="{{ $name }}"
value="1"
@if($value) checked @endif
id="checkbox_{{ $name }}">
<label class="form-check-label" for="checkbox_{{ $name }}">
{{ $right_title }}
</label>
</div>
</div>
</div>
The submitted form will always carry a field with the name {{ $name }}
.
This is handy for the request validation in Laravel.
You might call this inside blade template like:
{{-- Public Flag --}}
@include('bootstrap.forms.checkbox_boolean', [
'name' => 'public', 'value' => old('public', optional($category)->public),
'left_title' => 'Published', 'right_title' => 'Category is published
])