From 028a7fbbefeec974a6400edbd7dc3e948edbe885 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sat, 17 Mar 2018 12:20:53 +0100 Subject: [PATCH] Add space-separated save value type for `ItemListFormField` See #2509 --- .../builder/field/ItemListFormField.class.php | 84 +++++++++++++++++-- 1 file changed, 78 insertions(+), 6 deletions(-) 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 60e1d0f547..883802dd4a 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 @@ -2,6 +2,7 @@ declare(strict_types=1); namespace wcf\system\form\builder\field; use wcf\system\form\builder\field\data\CustomFormFieldDataProcessor; +use wcf\system\form\builder\field\validation\FormFieldValidationError; use wcf\system\form\builder\IFormDocument; use wcf\system\form\builder\IFormNode; use wcf\util\ArrayUtil; @@ -40,11 +41,30 @@ class ItemListFormField extends AbstractFormField { */ const SAVE_VALUE_TYPE_CSV = 'csv'; + /** + * save value return type so that space-separated list with the item values + * will be returned + * @var string + */ + const SAVE_VALUE_TYPE_SSV = 'ssv'; + /** * @inheritDoc */ public function getSaveValue() { - return is_array($this->getValue()) ? implode(',', $this->getValue()) : ''; + switch ($this->getSaveValueType()) { + case self::SAVE_VALUE_TYPE_ARRAY: + return ''; + + case self::SAVE_VALUE_TYPE_CSV: + return implode(',', $this->getValue()); + + case self::SAVE_VALUE_TYPE_SSV: + return implode(' ', $this->getValue()); + + default: + throw new \LogicException("Unreachable"); + } } /** @@ -66,8 +86,8 @@ class ItemListFormField extends AbstractFormField { * @inheritDoc */ public function hasSaveValue(): bool { - // only a string can be returned as a simple save value - return $this->getSaveValueType() === self::SAVE_VALUE_TYPE_CSV; + // arrays cannot be returned as a simple save value + return $this->getSaveValueType() !== self::SAVE_VALUE_TYPE_ARRAY; } /** @@ -104,7 +124,7 @@ class ItemListFormField extends AbstractFormField { /** * Sets the type of the returned save value (see `SAVE_VALUE_TYPE_*` constants). * - * @param string $saveValueTyp type of the returned save value + * @param string $saveValueType type of the returned save value * @return ItemListFormField this field * @throws \BadMethodCallException if save value type has already been set * @throws \InvalidArgumentException if given save value type is invalid @@ -114,7 +134,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) { + if ($saveValueType !== self::SAVE_VALUE_TYPE_ARRAY && $saveValueType !== self::SAVE_VALUE_TYPE_CSV && $saveValueType !== self::SAVE_VALUE_TYPE_SSV) { throw new \InvalidArgumentException("Unknown save value type '{$saveValueType}'."); } @@ -137,7 +157,7 @@ class ItemListFormField extends AbstractFormField { } break; - + case self::SAVE_VALUE_TYPE_CSV: if (is_string($value)) { $this->__value = explode(',', $value); @@ -147,6 +167,16 @@ class ItemListFormField extends AbstractFormField { } break; + + case self::SAVE_VALUE_TYPE_SSV: + if (is_string($value)) { + $this->__value = explode(' ', $value); + } + else { + throw new \InvalidArgumentException("Given value is no string, '" . gettype($value) . "' given."); + } + + break; default: throw new \LogicException("Unreachable"); @@ -154,4 +184,46 @@ class ItemListFormField extends AbstractFormField { return $this; } + + /** + * @inheritDoc + */ + public function validate() { + if (is_array($this->getValue())) { + $invalidItems = []; + foreach ($this->getValue() as $item) { + switch ($this->getSaveValueType()) { + case self::SAVE_VALUE_TYPE_CSV: + if (strpos($item, ',') !== false) { + $invalidItems[] = $item; + } + + break; + + case self::SAVE_VALUE_TYPE_SSV: + if (strpos($item, ' ') !== false) { + $invalidItems[] = $item; + } + + break; + + default: + throw new \LogicException("Unreachable"); + } + } + + if (!empty($invalidItems)) { + $this->addValidationError(new FormFieldValidationError( + 'separator', + 'wcf.form.field.itemList.error.separator', + [ + 'invalidItems' => $invalidItems, + 'separator' => $this->getSaveValueType() === self::SAVE_VALUE_TYPE_CSV ? ',' : ' ' + ] + )); + } + } + + parent::validate(); + } } -- 2.20.1