From 332ed6293166f07d81abb0689c1c9333b08acc6c Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Mon, 8 Apr 2019 14:37:36 +0800 Subject: [PATCH] Increment index count after new item is created Effectively starts the repeater item indexes from 0 --- modules/backend/formwidgets/Repeater.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/modules/backend/formwidgets/Repeater.php b/modules/backend/formwidgets/Repeater.php index d9d6be2cd..f2adcda28 100644 --- a/modules/backend/formwidgets/Repeater.php +++ b/modules/backend/formwidgets/Repeater.php @@ -233,8 +233,8 @@ class Repeater extends FormWidgetBase foreach ($items as $index => $groupCode) { $this->makeItemFormWidget($index, $groupCode); - $this->indexCount = max((int) $index, $this->indexCount); } + $this->indexCount = max(count($items), $this->indexCount); } /** @@ -291,8 +291,6 @@ class Repeater extends FormWidgetBase { self::$onAddItemCalled = true; - $this->indexCount++; - $groupCode = post('_repeater_group'); $this->prepareVars(); @@ -300,7 +298,13 @@ class Repeater extends FormWidgetBase $this->vars['indexValue'] = $this->indexCount; $itemContainer = '@#'.$this->getId('items'); - return [$itemContainer => $this->makePartial('repeater_item')]; + + // Increase index count after item is created + ++$this->indexCount; + + return [ + $itemContainer => $this->makePartial('repeater_item') + ]; } public function onRemoveItem()