From 983b1920669e17b41e23ff206544617091445681 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sun, 18 Aug 2019 10:23:39 +0200 Subject: [PATCH] Add maximum number of items support for ItemListFormField Close #3040 --- .../templates/__itemListFormField.tpl | 1 + .../acp/templates/__itemListFormField.tpl | 1 + .../builder/field/ItemListFormField.class.php | 31 ++++++++++++++++++- ...ectTypePackageInstallationPlugin.class.php | 2 +- wcfsetup/install/lang/de.xml | 3 +- wcfsetup/install/lang/en.xml | 1 + 6 files changed, 36 insertions(+), 3 deletions(-) diff --git a/com.woltlab.wcf/templates/__itemListFormField.tpl b/com.woltlab.wcf/templates/__itemListFormField.tpl index 19829c3347..5d95bdb34f 100644 --- a/com.woltlab.wcf/templates/__itemListFormField.tpl +++ b/com.woltlab.wcf/templates/__itemListFormField.tpl @@ -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()}[]' } ); diff --git a/wcfsetup/install/files/acp/templates/__itemListFormField.tpl b/wcfsetup/install/files/acp/templates/__itemListFormField.tpl index 19829c3347..5d95bdb34f 100644 --- a/wcfsetup/install/files/acp/templates/__itemListFormField.tpl +++ b/wcfsetup/install/files/acp/templates/__itemListFormField.tpl @@ -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()}[]' } ); 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 e04e6e784b..a2e598999e 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 @@ -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()) { diff --git a/wcfsetup/install/files/lib/system/package/plugin/ObjectTypePackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/ObjectTypePackageInstallationPlugin.class.php index 5a5f343600..e3c6137b9c 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/ObjectTypePackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/ObjectTypePackageInstallationPlugin.class.php @@ -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; diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index 2f1ad18ad9..fcd4328051 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -3892,7 +3892,7 @@ Dateianhänge: {$parentClass}.]]> - + @@ -3917,6 +3917,7 @@ Dateianhänge: + diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index 28ec2ad0b6..eb1a4bb0cf 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -3863,6 +3863,7 @@ Attachments: + -- 2.20.1