From: Tim Düsterhus Date: Thu, 4 May 2023 08:19:53 +0000 (+0200) Subject: Internally work with `null` in SelectFormField X-Git-Tag: 6.0.0_Alpha_1~161^2~1 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=20e4829bd4f34043095ca64f115cb68dacbcd2db;p=GitHub%2FWoltLab%2FWCF.git 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. --- 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);