From: Matthias Schmidt Date: Sun, 9 Sep 2018 12:50:58 +0000 (+0200) Subject: Add newline-separated save value for item list form field X-Git-Tag: 5.2.0_Alpha_1~674^2 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=16c6588e213cccb79704e976d8b8abcbed062b45;p=GitHub%2FWoltLab%2FWCF.git Add newline-separated save value for item list form field See #2509 --- diff --git a/wcfsetup/install/files/lib/system/form/builder/field/ItemListFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/ItemListFormField.class.php index c8ec995de0..d09880bb8f 100644 --- a/wcfsetup/install/files/lib/system/form/builder/field/ItemListFormField.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/field/ItemListFormField.class.php @@ -39,6 +39,13 @@ class ItemListFormField extends AbstractFormField { */ const SAVE_VALUE_TYPE_CSV = 'csv'; + /** + * save value return type so that newline-separated list with the item values + * will be returned + * @var string + */ + const SAVE_VALUE_TYPE_NSV = 'nsv'; + /** * save value return type so that space-separated list with the item values * will be returned @@ -57,6 +64,9 @@ class ItemListFormField extends AbstractFormField { case self::SAVE_VALUE_TYPE_CSV: return implode(',', $this->getValue() ?: []); + case self::SAVE_VALUE_TYPE_NSV: + return implode("\n", $this->getValue() ?: []); + case self::SAVE_VALUE_TYPE_SSV: return implode(' ', $this->getValue() ?: []); @@ -136,7 +146,7 @@ class ItemListFormField extends AbstractFormField { throw new \BadMethodCallException("Save value type has already been set."); } - if ($saveValueType !== self::SAVE_VALUE_TYPE_ARRAY && $saveValueType !== self::SAVE_VALUE_TYPE_CSV && $saveValueType !== self::SAVE_VALUE_TYPE_SSV) { + if (!in_array($saveValueType, [self::SAVE_VALUE_TYPE_ARRAY, self::SAVE_VALUE_TYPE_CSV, self::SAVE_VALUE_TYPE_NSV, self::SAVE_VALUE_TYPE_SSV])) { throw new \InvalidArgumentException("Unknown save value type '{$saveValueType}'."); } @@ -170,6 +180,16 @@ class ItemListFormField extends AbstractFormField { break; + case self::SAVE_VALUE_TYPE_NSV: + if (is_string($value)) { + $this->__value = explode("\n", $value); + } + else { + throw new \InvalidArgumentException("Given value is no string, '" . gettype($value) . "' given."); + } + + break; + case self::SAVE_VALUE_TYPE_SSV: if (is_string($value)) { $this->__value = explode(' ', $value); @@ -206,6 +226,13 @@ class ItemListFormField extends AbstractFormField { break; + case self::SAVE_VALUE_TYPE_NSV: + if (strpos($item, "\n") !== false) { + $invalidItems[] = $item; + } + + break; + case self::SAVE_VALUE_TYPE_SSV: if (strpos($item, ' ') !== false) { $invalidItems[] = $item; @@ -219,12 +246,27 @@ class ItemListFormField extends AbstractFormField { } if (!empty($invalidItems)) { + $separator = ''; + switch ($this->getSaveValue()) { + case self::SAVE_VALUE_TYPE_CSV: + $separator = ','; + break; + + case self::SAVE_VALUE_TYPE_NSV: + $separator = "\n"; + break; + + case self::SAVE_VALUE_TYPE_SSV: + $separator = ' '; + break; + } + $this->addValidationError(new FormFieldValidationError( 'separator', 'wcf.form.field.itemList.error.separator', [ 'invalidItems' => $invalidItems, - 'separator' => $this->getSaveValueType() === self::SAVE_VALUE_TYPE_CSV ? ',' : ' ' + 'separator' => $separator ] )); } diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index 2803504d52..5f9459f821 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -3042,7 +3042,7 @@ Email: {@$emailAddress} {* this line ends with a space *} {$parentClass}.]]> - +