Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Remove JS from checkbox #3043

Closed
wants to merge 2 commits into from
Closed
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
59 changes: 7 additions & 52 deletions src/resources/views/crud/fields/checkbox.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
@include('crud::fields.inc.wrapper_start')
@include('crud::fields.inc.translatable_icon')
<div class="checkbox">
<input type="hidden" name="{{ $field['name'] }}" value="{{ old(square_brackets_to_dots($field['name'])) ?? $field['value'] ?? $field['default'] ?? 0 }}">
<input type="hidden" name="{{ $field['name'] }}" value="0">
<label class="form-check-label font-weight-normal">
<input type="checkbox"
data-init-function="bpFieldInitCheckbox"

name="{{ $field['name'] }}"
value="1"
@if (old(square_brackets_to_dots($field['name'])) ?? $field['value'] ?? $field['default'] ?? false)
checked="checked"
checked
@endif

@if (isset($field['attributes']))
Expand All @@ -17,57 +18,11 @@
@endforeach
@endif
>
<label class="form-check-label font-weight-normal">{!! $field['label'] !!}</label>
{!! $field['label'] !!}</label>

{{-- HINT --}}
@if (isset($field['hint']))
<p class="help-block">{!! $field['hint'] !!}</p>
@endif
</div>
@include('crud::fields.inc.wrapper_end')

{{-- ########################################## --}}
{{-- Extra CSS and JS for this particular field --}}
{{-- If a field type is shown multiple times on a form, the CSS and JS will only be loaded once --}}
@if ($crud->fieldTypeNotLoaded($field))
@php
$crud->markFieldTypeAsLoaded($field);
@endphp
{{-- FIELD JS - will be loaded in the after_scripts section --}}
@push('crud_fields_scripts')
<script>
function bpFieldInitCheckbox(element) {
var hidden_element = element.siblings('input[type=hidden]');
var id = 'checkbox_'+Math.floor(Math.random() * 1000000);

// make sure the value is a boolean (so it will pass validation)
if (hidden_element.val() === '') hidden_element.val(0);

// set unique IDs so that labels are correlated with inputs
element.attr('id', id);
element.siblings('label').attr('for', id);

// set the default checked/unchecked state
// if the field has been loaded with javascript
if (hidden_element.val() != 0) {
element.prop('checked', 'checked');
} else {
element.prop('checked', false);
}

// when the checkbox is clicked
// set the correct value on the hidden input
element.change(function() {
if (element.is(":checked")) {
hidden_element.val(1);
} else {
hidden_element.val(0);
}
})
}
</script>
@endpush

@endif
{{-- End of Extra CSS and JS --}}
{{-- ########################################## --}}
@include('crud::fields.inc.wrapper_end')