From 20e4829bd4f34043095ca64f115cb68dacbcd2db Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Thu, 4 May 2023 10:19:53 +0200 Subject: [PATCH] Internally work with `null` in SelectFormField Convert the empty string to `null` immediately when reading the data to allow custom validators to check for the `null` value instead of the empty string. --- .../builder/field/SelectFormField.class.php | 28 ++++--------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/wcfsetup/install/files/lib/system/form/builder/field/SelectFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/SelectFormField.class.php index f35a2d7369..6d997a37c4 100644 --- a/wcfsetup/install/files/lib/system/form/builder/field/SelectFormField.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/field/SelectFormField.class.php @@ -30,18 +30,6 @@ final class SelectFormField extends AbstractFormField implements */ protected $templateName = '__selectFormField'; - /** - * @inheritDoc - */ - public function getSaveValue() - { - if ($this->getValue() === '') { - return; - } - - return parent::getSaveValue(); - } - /** * @inheritDoc */ @@ -51,7 +39,7 @@ final class SelectFormField extends AbstractFormField implements $value = $this->getDocument()->getRequestData($this->getPrefixedId()); if (\is_string($value)) { - $this->value = $value; + $this->value = $value !== '' ? $value : null; } } @@ -63,7 +51,7 @@ final class SelectFormField extends AbstractFormField implements */ public function validate() { - if ($this->getValue() === '') { + if ($this->getValue() === null) { if ($this->isRequired()) { $this->addValidationError(new FormFieldValidationError('empty')); } @@ -82,14 +70,10 @@ final class SelectFormField extends AbstractFormField implements */ public function value($value) { - // ignore `null` as value which can be passed either for nullable - // fields or as value if no options are available - if ($value === null) { - return $this; - } - - if (!isset($this->getOptions()[$value])) { - throw new \InvalidArgumentException("Unknown value '{$value}' for field '{$this->getId()}'."); + if ($value !== null) { + if (!isset($this->getOptions()[$value])) { + throw new \InvalidArgumentException("Unknown value '{$value}' for field '{$this->getId()}'."); + } } return parent::value($value); -- 2.20.1