From 9b16b11bea1d4a602cc272b821c05eaa5c758b23 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Thu, 22 Aug 2019 17:37:08 +0200 Subject: [PATCH] ItemListFormField implements IMultipleFormField instead of IMaximumFormField --- .../templates/__itemListFormField.tpl | 2 +- .../acp/templates/__itemListFormField.tpl | 2 +- .../builder/field/ItemListFormField.class.php | 48 +++++++++---------- wcfsetup/install/lang/de.xml | 3 +- wcfsetup/install/lang/en.xml | 3 +- 5 files changed, 30 insertions(+), 28 deletions(-) diff --git a/com.woltlab.wcf/templates/__itemListFormField.tpl b/com.woltlab.wcf/templates/__itemListFormField.tpl index 5d95bdb34f..7c43cc017a 100644 --- a/com.woltlab.wcf/templates/__itemListFormField.tpl +++ b/com.woltlab.wcf/templates/__itemListFormField.tpl @@ -8,7 +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}, + maxItems: {if $field->allowsMultiple()}{@$field->getMaximumMultiples()}{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 5d95bdb34f..7c43cc017a 100644 --- a/wcfsetup/install/files/acp/templates/__itemListFormField.tpl +++ b/wcfsetup/install/files/acp/templates/__itemListFormField.tpl @@ -8,7 +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}, + maxItems: {if $field->allowsMultiple()}{@$field->getMaximumMultiples()}{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 a2e598999e..a82bde0ff4 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,12 +14,10 @@ use wcf\util\ArrayUtil; * @package WoltLabSuite\Core\System\Form\Builder\Field * @since 5.2 */ -class ItemListFormField extends AbstractFormField implements IAutoFocusFormField, IImmutableFormField, IMaximumFormField { +class ItemListFormField extends AbstractFormField implements IAutoFocusFormField, IImmutableFormField, IMultipleFormField { use TAutoFocusFormField; use TImmutableFormField; - use TMaximumFormField { - maximum as protected traitMaximum; - } + use TMultipleFormField; /** * @inheritDoc @@ -64,6 +62,13 @@ class ItemListFormField extends AbstractFormField implements IAutoFocusFormField */ const SAVE_VALUE_TYPE_SSV = 'ssv'; + /** + * @inheritDoc + */ + public function __construct() { + $this->multiple(); + } + /** * @inheritDoc */ @@ -109,19 +114,6 @@ 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 */ @@ -236,17 +228,25 @@ class ItemListFormField extends AbstractFormField implements IAutoFocusFormField */ public function validate() { if (is_array($this->getValue())) { - if ($this->getMaximum() !== null && count($this->getValue()) > $this->getMaximum()) { + if ($this->getMinimumMultiples() > 0 && count($this->getValue()) < $this->getMinimumMultiples()) { $this->addValidationError(new FormFieldValidationError( - 'maximumItems', - 'wcf.form.field.itemList.error.maximumItems', + 'minimumMultiples', + 'wcf.form.field.itemList.error.minimumMultiples', [ - 'items' => count($this->getValue()), - 'maximumItems' => $this->getMaximum() + 'minimumCount' => $this->getMinimumMultiples(), + 'count' => count($this->getValue()) + ] + )); + } + else if ($this->getMaximumMultiples() !== IMultipleFormField::NO_MAXIMUM_MULTIPLES && count($this->getValue()) > $this->getMaximumMultiples()) { + $this->addValidationError(new FormFieldValidationError( + 'maximumMultiples', + 'wcf.form.field.itemList.error.maximumMultiples', + [ + 'maximumCount' => $this->getMaximumMultiples(), + 'count' => count($this->getValue()) ] )); - - return; } $invalidItems = []; diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index 34ef2900c9..121d696d32 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -3917,7 +3917,8 @@ Dateianhänge: - + + diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index 2714015cc3..0501592af8 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -3863,7 +3863,8 @@ Attachments: - + + -- 2.20.1