Allow the checkbox form field to be nullable
authorMarcel Werk <burntime@woltlab.com>
Wed, 11 Dec 2024 17:30:27 +0000 (18:30 +0100)
committerMarcel Werk <burntime@woltlab.com>
Wed, 11 Dec 2024 17:30:27 +0000 (18:30 +0100)
wcfsetup/install/files/lib/system/form/builder/field/CheckboxFormField.class.php

index ce0951f67ae17b5756af6e74254194c321aefe57..05dcc49e4f79efb7918cd9de67d42c9d6301575e 100644 (file)
@@ -12,16 +12,16 @@ use wcf\system\WCF;
  * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @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);
+    }
 }