diff --git a/lib/src/form/src/controller.dart b/lib/src/form/src/controller.dart index ec3a6f9..7b60036 100644 --- a/lib/src/form/src/controller.dart +++ b/lib/src/form/src/controller.dart @@ -196,10 +196,22 @@ class AstromicFormController extends FormController { } int getInstanceCount(String targetGroupID) { + String? p = getFullPathOfGroup(targetGroupID); + if (p != null && p.split('->').length > 1 && p.split('->').indexOf(targetGroupID) != 0) { + // This is a subgroup + } FormGroupStructure? structure = getGroupStructure(targetGroupID); if (structure != null) { String firstField = structure.fields.first; - return controllers.keys.where((String c) => RegExp(standeredGroupFormat(targetGroupID, r'[\d+]', firstField)).hasMatch(c)).nonNulls.toList().length; + // return controllers.keys.where((String c) => RegExp(standeredGroupFormat(targetGroupID, r'[\d+]', firstField)).hasMatch(c)).nonNulls.toList().length; + return controllers.keys + .where((String c) { + // Match the specific group ID and field name within the group + final RegExp pattern = RegExp(r'^' + RegExp.escape(standeredGroupFormat(targetGroupID, r'[\d+]', firstField)) + r'(-#\d+)?$'); + return pattern.hasMatch(c); + }) + .nonNulls + .toList().length; } return 0; }