From abbe07c500d7a74eabc7a14d6db11bab49b82d6b Mon Sep 17 00:00:00 2001 From: Michael Aziz Date: Sat, 12 Apr 2025 15:09:35 +0200 Subject: [PATCH] [SYNC] --- lib/src/form/src/controller.dart | 41 +++++++++++++++++++------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/lib/src/form/src/controller.dart b/lib/src/form/src/controller.dart index e5914a5..e1f7811 100644 --- a/lib/src/form/src/controller.dart +++ b/lib/src/form/src/controller.dart @@ -121,6 +121,14 @@ class AstromicFormController extends FormController { _hostedValueValidationStreamController.add((id, false)); } + /// Remove the value of a hosted state variable using it's ID. + void removeValue(String id) { + if (!_hostedValues.keys.toList().contains(id)) { + throw Exception('Value of id `$id` not found to be removed.'); + } + _hostedValues.remove(id); + } + /// Validate hosted values. bool validateValues(List valueIDs) { for (String hostedValueID in valueIDs) { @@ -331,35 +339,34 @@ class AstromicFormController extends FormController { return fullPath != null && fullPath.length > 1 && fullPath.indexOf(formGroupID) != 0; } - String addInstanceToFormGroup(String formGroupID, {Map? parentsPrefixes}) { + String addInstanceToFormGroup(String formGroupID, {Map? parents}) { // Get the group structure with the same ID FormGroupStructure? structure = getGroupStructure(formGroupID); assert(structure != null, 'The Group $formGroupID doesn\'t seem to be found, are you sure you initialized it?'); - // Get current instances of this group - int currentGroupInstances = getInstanceCount(formGroupID); - int newGroupIndex = currentGroupInstances + 1; - - if (structure!.subGroups != null && structure.subGroups!.isNotEmpty) { - for ((int gI, FormGroupStructure gS) f in structure.subGroups!) { - addInstanceToFormGroup(formGroupID, parentsPrefixes: { - ...?parentsPrefixes, - ...{formGroupID: newGroupIndex}, - }); - } + // Get the prefix for subgroups if exists + String? prefix; + if (isASubGroup(formGroupID) && parents != null) { + prefix = parents.entries.map((MapEntry parentEntry) => standeredGroupFormat(parentEntry.key, parentEntry.value.toString(), null)).join('-'); } - String prefixedGroup = parentsPrefixes?.entries.map((MapEntry p) => standeredGroupFormat(p.key, p.value.toString(), null)).toList().join('-') ?? ''; + // Get current instances of this group and the new index. + int currentGroupInstances = getInstanceCount(formGroupID); + int newGroupIndex = currentGroupInstances; + // Add the controllers and values. - for (String fieldID in structure.fields.nonNulls) { - controller(prefixedGroup + standeredGroupFormat(formGroupID, newGroupIndex.toString(), fieldID)); + for (String fieldID in structure!.fields.nonNulls) { + String p = (prefix != null ? '$prefix-' : '') + standeredGroupFormat(formGroupID, newGroupIndex.toString(), fieldID); + controller(p); } if (structure.values != null && structure.values!.isNotEmpty) { for (String valueID in structure.values!.nonNulls) { - controller(prefixedGroup + standeredGroupFormat(formGroupID, newGroupIndex.toString(), valueID)); + String p = (prefix != null ? '$prefix-' : '') + standeredGroupFormat(formGroupID, newGroupIndex.toString(), valueID); + controller(p); } } - return prefixedGroup + standeredGroupFormat(formGroupID, newGroupIndex.toString(), ''); + String p = (prefix != null ? '$prefix-' : '') + standeredGroupFormat(formGroupID, newGroupIndex.toString(), ''); + return p; } // void removeInstanceFromFormGroup(String formGroupID, int indexToRemove, {bool isSubGroup = false}) {