From 972839286c486d84f2a1a20712be1262afd7b485 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Wed, 30 Sep 2026 17:42:21 +0200 Subject: [PATCH] fix: never reuse a collection entry index after a removal --- assets/controllers/form_collection_controller.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/assets/controllers/form_collection_controller.js b/assets/controllers/form_collection_controller.js index 2de5acdb..344f2262 100644 --- a/assets/controllers/form_collection_controller.js +++ b/assets/controllers/form_collection_controller.js @@ -11,10 +11,13 @@ export default class extends Controller { connect() { this.itemsCountValue = this.fieldTargets.length + // A running counter rather than the current count: after a remove, count + 1 can be an + // index a remaining entry still holds, and the two would submit into the same slot. + this.nextIndex = this.fieldTargets.length } addItem() { - const index = this.fieldTargets.length + 1 + const index = this.nextIndex++ const prototype = JSON.parse(this.prototypeValue) const newField = prototype.replace(/__name__/g, index) this.fieldsTarget.insertAdjacentHTML('beforeend', newField)