From c1d9c9baa6ff09f696b0255eeb8c12209f497e05 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Thu, 12 Jul 2018 19:14:52 +0200 Subject: [PATCH] Fix value and validation-related issues in TI18nFormField See #2509 --- .../builder/field/TI18nFormField.class.php | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/wcfsetup/install/files/lib/system/form/builder/field/TI18nFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/TI18nFormField.class.php index ed88f6e715..b2182a4484 100644 --- a/wcfsetup/install/files/lib/system/form/builder/field/TI18nFormField.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/field/TI18nFormField.class.php @@ -9,6 +9,7 @@ use wcf\system\form\builder\IFormDocument; use wcf\system\form\builder\IFormNode; use wcf\system\language\I18nHandler; use wcf\system\Regex; +use wcf\util\ArrayUtil; use wcf\util\StringUtil; /** @@ -340,7 +341,9 @@ trait TI18nFormField { $this->setStringValue($value); } else if (is_array($value)) { - I18nHandler::getInstance()->setValues($this->getPrefixedId(), $value); + if (!empty($value)) { + I18nHandler::getInstance()->setValues($this->getPrefixedId(), $value); + } } else { throw new \InvalidArgumentException("Given value is neither a string nor an array, " . gettype($value) . " given."); @@ -364,12 +367,16 @@ trait TI18nFormField { * nodes are valid. A `IFormField` object is valid if its value is valid. */ public function validate() { - if (!I18nHandler::getInstance()->validateValue($this->getPrefixedId(), $this->isI18nRequired(), !$this->isRequired())) { - if ($this->hasPlainValue()) { - $this->addValidationError(new FormFieldValidationError('empty')); - } - else { - $this->addValidationError(new FormFieldValidationError('multilingual')); + // if i18n is required for a non-required field and the field is + // empty, that is no error + if ($this->isI18n() && (!empty(ArrayUtil::trim($this->getValue()))) || $this->isRequired()) { + if (!I18nHandler::getInstance()->validateValue($this->getPrefixedId(), $this->isI18nRequired(), !$this->isRequired())) { + if ($this->hasPlainValue()) { + $this->addValidationError(new FormFieldValidationError('empty')); + } + else { + $this->addValidationError(new FormFieldValidationError('multilingual')); + } } } } -- 2.20.1