Add maximum number of items support for ItemListFormField
authorMatthias Schmidt <gravatronics@live.com>
Sun, 18 Aug 2019 08:23:39 +0000 (10:23 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sun, 18 Aug 2019 08:23:39 +0000 (10:23 +0200)
Close #3040

com.woltlab.wcf/templates/__itemListFormField.tpl
wcfsetup/install/files/acp/templates/__itemListFormField.tpl
wcfsetup/install/files/lib/system/form/builder/field/ItemListFormField.class.php
wcfsetup/install/files/lib/system/package/plugin/ObjectTypePackageInstallationPlugin.class.php
wcfsetup/install/lang/de.xml
wcfsetup/install/lang/en.xml

index 19829c3347050ed018a5c985d7dfcd922632ce9b..5d95bdb34f9a7eceb294cfd84b081726b3d68f0c 100644 (file)
@@ -8,6 +8,7 @@
                        '{@$field->getPrefixedId()}',
                        [{if $field->getValue() !== null && !$field->getValue()|empty}{implode from=$field->getValue() item=item}'{@$item|encodeJS}'{/implode}{/if}],
                        {
+                               maxItems: {if $field->getMaximum() !== null}{@$field->getMaximum()}{else}-1{/if},
                                submitFieldName: '{@$field->getPrefixedId()}[]'
                        }
                );
index 19829c3347050ed018a5c985d7dfcd922632ce9b..5d95bdb34f9a7eceb294cfd84b081726b3d68f0c 100644 (file)
@@ -8,6 +8,7 @@
                        '{@$field->getPrefixedId()}',
                        [{if $field->getValue() !== null && !$field->getValue()|empty}{implode from=$field->getValue() item=item}'{@$item|encodeJS}'{/implode}{/if}],
                        {
+                               maxItems: {if $field->getMaximum() !== null}{@$field->getMaximum()}{else}-1{/if},
                                submitFieldName: '{@$field->getPrefixedId()}[]'
                        }
                );
index e04e6e784be43387bd98c62bd0353d27fae7c71d..a2e598999e33af59a7593cff489ae3f560ce0c94 100644 (file)
@@ -14,9 +14,12 @@ use wcf\util\ArrayUtil;
  * @package    WoltLabSuite\Core\System\Form\Builder\Field
  * @since      5.2
  */
-class ItemListFormField extends AbstractFormField implements IAutoFocusFormField, IImmutableFormField {
+class ItemListFormField extends AbstractFormField implements IAutoFocusFormField, IImmutableFormField, IMaximumFormField {
        use TAutoFocusFormField;
        use TImmutableFormField;
+       use TMaximumFormField {
+               maximum as protected traitMaximum;
+       }
        
        /**
         * @inheritDoc
@@ -106,6 +109,19 @@ class ItemListFormField extends AbstractFormField implements IAutoFocusFormField
                return $this->getSaveValueType() !== self::SAVE_VALUE_TYPE_ARRAY;
        }
        
+       /**
+        * @inheritDoc
+        */
+       public function maximum($maximum = null) {
+               $this->traitMaximum($maximum);
+               
+               if ($maximum <= 0) {
+                       throw new \InvalidArgumentException("The maximum number of items has to be positive.");
+               }
+               
+               return $this;
+       }
+       
        /**
         * @inheritDoc
         */
@@ -220,6 +236,19 @@ class ItemListFormField extends AbstractFormField implements IAutoFocusFormField
         */
        public function validate() {
                if (is_array($this->getValue())) {
+                       if ($this->getMaximum() !== null && count($this->getValue()) > $this->getMaximum()) {
+                               $this->addValidationError(new FormFieldValidationError(
+                                       'maximumItems',
+                                       'wcf.form.field.itemList.error.maximumItems',
+                                       [
+                                               'items' => count($this->getValue()),
+                                               'maximumItems' => $this->getMaximum()
+                                       ]
+                               ));
+                               
+                               return;
+                       }
+                       
                        $invalidItems = [];
                        foreach ($this->getValue() as $item) {
                                switch ($this->getSaveValueType()) {
index 5a5f34360011154ba3fe379cbb9b44015a05a663..e3c6137b9cac2137e4154f7c4f1bd070014e6886 100644 (file)
@@ -481,11 +481,11 @@ class ObjectTypePackageInstallationPlugin extends AbstractXMLPackageInstallation
                // com.woltlab.wcf.message
                $this->getObjectTypeDefinitionDataContainer($form, 'com.woltlab.wcf.message')
                        ->appendChildren([
-                               // TODO: maximum of one item (see https://github.com/WoltLab/WCF/issues/3040)
                                UserGroupOptionFormField::create('messageObjectDisallowedBBCodesPermission')
                                        ->objectProperty('disallowedBBCodesPermission')
                                        ->label('wcf.acp.pip.objectType.com.woltlab.wcf.message.disallowedBBCodesPermission')
                                        ->description('wcf.acp.pip.objectType.com.woltlab.wcf.message.disallowedBBCodesPermission.description')
+                                       ->maximum(1)
                                        ->addValidator(new FormFieldValidator('optionType', function(UserGroupOptionFormField $formField) {
                                                $value = $formField->getValue();
                                                if (empty($value)) return;
index 2f1ad18ad9ec7c0d7f5c9c6fbf40a65a4e64ac3e..fcd4328051522516eea3836de51c048638a37e9d 100644 (file)
@@ -3892,7 +3892,7 @@ Dateianhänge:
                <item name="wcf.form.field.className.error.leadingBackslash"><![CDATA[Die angegebene Klasse{if $language|isset} ({$language}){/if} darf keinen führenden Backslash besitzen.]]></item>
                <item name="wcf.form.field.className.error.nonExistent"><![CDATA[Die angegebene Klasse{if $language|isset} ({$language}){/if} existiert nicht.]]></item>
                <item name="wcf.form.field.className.error.parentClass"><![CDATA[Die angegebene Klasse{if $language|isset} ({$language}){/if} erbt nicht von <kbd>{$parentClass}</kbd>.]]></item>
-               <item name="wcf.form.field.itemList.error.separator"><![CDATA[Die folgenden Elemente dürfen kein {if $separator === ','}Komma{else}Leerzeichen{/if} enthalten: {implode from=$invalidItems item=item}„{$item}“{/implode}.]]></item>
+               <item name="wcf.form.field.itemList.error.separator"><![CDATA[Die folgenden Einträge dürfen kein {if $separator === ','}Komma{else}Leerzeichen{/if} enthalten: {implode from=$invalidItems item=item}„{$item}“{/implode}.]]></item>
                <item name="wcf.form.field.numeric.error.maximum"><![CDATA[Der angegebene Wert darf nicht kleiner als {#$maximum} sein.]]></item>
                <item name="wcf.form.field.numeric.error.minimum"><![CDATA[Der angegebene Wert darf nicht größer als {#$minimum} sein.]]></item>
                <item name="wcf.form.field.option"><![CDATA[Einstellungen]]></item>
@@ -3917,6 +3917,7 @@ Dateianhänge:
                <item name="wcf.form.field.date.error.format"><![CDATA[Der angegebene Wert hat ein ungültiges Format.]]></item>
                <item name="wcf.form.field.date.error.earliestDate"><![CDATA[Der angegebene Wert darf nicht früher sein als {$earliestDate}.]]></item>
                <item name="wcf.form.field.date.error.latestDate"><![CDATA[Der angegebene Wert darf nicht später sein als {$latestDate}.]]></item>
+               <item name="wcf.form.field.itemList.error.maximumItems"><![CDATA[Die haben {#$items} Einträge angegebenen, die Maximalanzahl beträgt jedoch jedoch {#$maximumItems}.]]></item>
        </category>
        <category name="wcf.image">
                <item name="wcf.image.coverPhoto"><![CDATA[Titelbild]]></item>
index 28ec2ad0b60d444715d37267e3a7f75f04d4a696..eb1a4bb0cfe730834cea1f9e08888f91016b3936 100644 (file)
@@ -3863,6 +3863,7 @@ Attachments:
                <item name="wcf.form.field.date.error.format"><![CDATA[The format of the entered value is invalid.]]></item>
                <item name="wcf.form.field.date.error.earliestDate"><![CDATA[The entered value may not be earlier than {$earliestDate}.]]></item>
                <item name="wcf.form.field.date.error.latestDate"><![CDATA[The entered value may not be later than {$latestDate}.]]></item>
+               <item name="wcf.form.field.itemList.error.maximumItems"><![CDATA[You have entered {#$items} items but the maximum number of items is {#$maximumItems}.]]></item>
        </category>
        <category name="wcf.image">
                <item name="wcf.image.coverPhoto"><![CDATA[Cover Photo]]></item>