From 252a1b40b0c75c7db3199e2505c4aa7be155040d Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Wed, 11 Dec 2024 18:30:27 +0100 Subject: [PATCH] Allow the checkbox form field to be nullable --- .../builder/field/CheckboxFormField.class.php | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/wcfsetup/install/files/lib/system/form/builder/field/CheckboxFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/CheckboxFormField.class.php index ce0951f67a..05dcc49e4f 100644 --- a/wcfsetup/install/files/lib/system/form/builder/field/CheckboxFormField.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/field/CheckboxFormField.class.php @@ -12,16 +12,16 @@ use wcf\system\WCF; * @license GNU Lesser General Public License * @since 5.3 */ -class CheckboxFormField extends BooleanFormField +class CheckboxFormField extends BooleanFormField implements INullableFormField { + use TNullableFormField; + /** * @inheritDoc */ protected $javaScriptDataHandlerModule = 'WoltLabSuite/Core/Form/Builder/Field/CheckedVoid'; - /** - * @inheritDoc - */ + #[\Override] public function readValue() { $this->value = $this->getDocument()->hasRequestData($this->getPrefixedId()); @@ -29,9 +29,17 @@ class CheckboxFormField extends BooleanFormField return $this; } - /** - * @inheritDoc - */ + #[\Override] + public function getSaveValue() + { + if ($this->isNullable() && !$this->value) { + return null; + } + + return parent::getSaveValue(); + } + + #[\Override] public function getHtml() { if ($this->requiresLabel() && $this->getLabel() === null) { @@ -46,4 +54,14 @@ class CheckboxFormField extends BooleanFormField ] ); } + + #[\Override] + public function value($value) + { + if ($this->isNullable() && $value === null) { + $value = 0; + } + + return parent::value($value); + } } -- 2.20.1