Add newline-separated save value for item list form field
authorMatthias Schmidt <gravatronics@live.com>
Sun, 9 Sep 2018 12:50:58 +0000 (14:50 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sun, 9 Sep 2018 12:50:58 +0000 (14:50 +0200)
See #2509

wcfsetup/install/files/lib/system/form/builder/field/ItemListFormField.class.php
wcfsetup/install/lang/en.xml

index c8ec995de05ce901b5476ae3430c676ff77e278b..d09880bb8f6a00b17102d9259810a2de5ef1d546 100644 (file)
@@ -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
                                        ]
                                ));
                        }
index 2803504d520248dd34aae2cc145f811a2c4fb442..5f9459f821c8a542ea33cb1b08cc3218373f5f02 100644 (file)
@@ -3042,7 +3042,7 @@ Email: {@$emailAddress} {* this line ends with a space *}
                <item name="wcf.form.field.className.error.leadingBackslash"><![CDATA[The entered class name{if $language|isset} ({$language}){/if} must not have a leading backslash.]]></item>
                <item name="wcf.form.field.className.error.nonExistent"><![CDATA[The entered class{if $language|isset} ({$language}){/if} does not exist.]]></item>
                <item name="wcf.form.field.className.error.parentClass"><![CDATA[The entered class{if $language|isset} ({$language}){/if} does not extend <code>{$parentClass}</code>.]]></item>
-               <item name="wcf.form.field.itemList.error.separator"><![CDATA[The following items may contain no {if $separator === ','}comma{else}space{/if}: {implode from=$invalidItems item=item}“{$item}”{/implode}.]]></item>
+               <item name="wcf.form.field.itemList.error.separator"><![CDATA[The following items may contain no {if $separator === ','}comma{else if $separator === "\n"}newline{else}space{/if}: {implode from=$invalidItems item=item}“{$item}”{/implode}.]]></item>
                <item name="wcf.form.field.numeric.error.maximum"><![CDATA[The entered value may not be less than {#$maximum}.]]></item>
                <item name="wcf.form.field.numeric.error.minimum"><![CDATA[The entered value may not be greater than {#$minimum}.]]></item>
                <item name="wcf.form.field.showOrder"><![CDATA[Position]]></item>