Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Checkbox value can be unset #2172

Merged
merged 1 commit into from
Nov 26, 2020
Merged
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
55 changes: 55 additions & 0 deletions assets/js/app/editor/Components/Checkbox.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<template>
<div>
<div :class="['custom-control', 'custom-' + mode]">
<input
:id="id"
class="custom-control-input"
:name="name"
:checked="value"
type="checkbox"
:readonly="readonly"
@change="liveValue = $event.target.checked"
/>
<label class="custom-control-label" :for="name">{{ label }}</label>

<!-- This hidden input is actually what gets submitted. It submits "true" when checked, and "false" when not checked -->
<!-- It exists because we need an "unchecked" value submitted. See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox -->
-->
<input type="hidden" :value="liveValue" :name="name" />
</div>
</div>
</template>

<script>
export default {
name: 'EditorCheckbox',
props: {
value: {
type: Boolean,
},
name: {
type: String,
},
id: {
type: String,
},
required: {
type: Boolean,
},
readonly: {
type: Boolean,
},
label: {
type: String,
},
mode: {
type: String,
},
},
data() {
return {
liveValue: this.value,
};
},
};
</script>
2 changes: 2 additions & 0 deletions assets/js/app/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import Language from './Components/Language';
import File from './Components/File';
import Filelist from './Components/Filelist';
import Collection from './Components/Collection';
import Checkbox from './Components/Checkbox';

Vue.component('editor-checkbox', Checkbox);
Vue.component('editor-date', Date);
Vue.component('editor-embed', Embed);
Vue.component('editor-email', Email);
Expand Down
17 changes: 17 additions & 0 deletions src/Entity/Field/CheckboxField.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ class CheckboxField extends Field implements FieldInterface, ScalarCastable, Raw
{
public const TYPE = 'checkbox';

public function setValue($value): Field
{
switch ($value) {
// String values come from the ContentEditController.
case 'true':
$value = true;
break;
case 'false':
$value = false;
break;
default:
$value = $value ? true : false;
}

return parent::setValue($value);
}

public function getTwigValue()
{
return current($this->getValue());
Expand Down
18 changes: 9 additions & 9 deletions templates/_partials/fields/checkbox.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
{% set mode = field.definition.mode|default('checkbox') %}
{% endif %}

<div class="custom-control custom-{{ mode }}">
{# What the heck is this hidden field for, you might wonder? Well, it's
here to make sure an unchecked checkbox still shows up in the server
response, so we can handle it there, if present. #}
<input type="hidden" name="{{ name }}" value="{{ value ? 'checked' }}">
<input class="custom-control-input" id="{{ name }}" name="{{ name }}" type="checkbox"
value="1" {{ value ? 'checked' }} {% if readonly %}disabled{% endif %}>
<label class="custom-control-label" for="{{ name }}">{{ label }}</label>
</div>
<editor-checkbox
:id="{{ name|json_encode }}"
:value="{{ value|json_encode }}"
:name="{{ name|json_encode }}"
:required="{{ required|json_encode }}"
:readonly="{{ readonly|json_encode }}"
:label="{{ label|json_encode }}"
:mode="{{ mode|json_encode }}"
></editor-checkbox>
{% endblock %}