diff --git a/lib/src/form/src/controller.dart b/lib/src/form/src/controller.dart index 9540647..7eccb54 100644 --- a/lib/src/form/src/controller.dart +++ b/lib/src/form/src/controller.dart @@ -147,8 +147,8 @@ class AstromicFormController extends FormController { if (group.id == targetGroupID) return '$currentPath${group.id}'; // Otherwise, check in its subgroups recursively - for (final (FormGroupStructure, int) subGroup in group.subGroups ?? <(FormGroupStructure, int)>[]) { - final String? subGroupPath = getFullPathOfGroup(targetGroupID, formGroups: [subGroup.$1], currentPath: '$currentPath${group.id}->'); + for (final (int, FormGroupStructure) subGroup in group.subGroups ?? <(int, FormGroupStructure)>[]) { + final String? subGroupPath = getFullPathOfGroup(targetGroupID, formGroups: [subGroup.$2], currentPath: '$currentPath${group.id}->'); // Return the path if found if (subGroupPath != null) { return subGroupPath; @@ -219,11 +219,11 @@ class AstromicFormController extends FormController { final String segment = pathSegments[i]; // Search for the subgroup within the current group - final (FormGroupStructure, int)? subGroup = currentGroup?.subGroups?.where(((FormGroupStructure, int) subGroup) => subGroup.$1.id == segment).firstOrNull; + final (int, FormGroupStructure)? subGroup = currentGroup?.subGroups?.where(((int, FormGroupStructure) subGroup) => subGroup.$2.id == segment).firstOrNull; // If a subgroup is found, update currentGroup to the subgroup if (subGroup != null) { - currentGroup = subGroup.$1; + currentGroup = subGroup.$2; } else { // If no subgroup is found at this level, return null return null; @@ -272,20 +272,6 @@ class AstromicFormController extends FormController { // } // } - void _addGroupControllers(FormGroupStructure structure, int index, {String? parentPrefix}) { - final String baseID = parentPrefix ?? structure.id; - - for (final String fieldID in structure.fields) { - final String fullID = standeredGroupFormat(baseID, index, fieldID); - controller(fullID); - } - - for (final String valueID in structure.values ?? []) { - final String fullID = standeredGroupFormat(baseID, index, valueID); - controller(fullID); - } - } - /// Initialize the formGroup Structure. // void initializeFormGroup(FormGroupStructure groupStructure, {int initialCount = 1}) { // assert(groupStructure.fields.isNotEmpty, '${groupStructure.id}: Group fields should NOT be empty.'); @@ -314,52 +300,66 @@ class AstromicFormController extends FormController { // } // } - void initializeFormGroup(FormGroupStructure groupStructure, {int initialCount = 1}) { - assert(groupStructure.fields.isNotEmpty, '${groupStructure.id}: Group fields should NOT be empty.'); + /// Validate subgroups recursively + void _validateSubGroupsRecursively(FormGroupStructure groupStructure) { + groupStructure.subGroups?.forEach(((int, FormGroupStructure) subGroupTuple) { + final FormGroupStructure subGroup = subGroupTuple.$2; + assert(subGroup.fields.isNotEmpty, '${subGroup.id}: Subgroup fields should NOT be empty.'); - // Validate subgroups (if any) - _validateSubGroups(groupStructure); + // Recursively validate subgroups of subgroups + if (subGroup.subGroups != null) { + _validateSubGroupsRecursively(subGroup); + } + }); + } - // Add structure to registry - _formGroups.add(groupStructure); + /// Add controllers + void _addGroupControllers(FormGroupStructure structure, int index, {String? parentPrefix}) { + final String baseID = parentPrefix ?? structure.id; - // Initialize the group instances - _initializeGroupControllers(groupStructure, initialCount); + for (final String fieldID in structure.fields) { + final String fullID = standeredGroupFormat(baseID, index, fieldID); + controller(fullID); + } + + for (final String valueID in structure.values ?? []) { + final String fullID = standeredGroupFormat(baseID, index, valueID); + controller(fullID); + } } /// Recursively initialize controllers for the group and its subgroups - void _initializeGroupControllers(FormGroupStructure groupStructure, int initialCount, {String parentPrefix = ''}) { + void _initializeGroupControllersRecursively(FormGroupStructure groupStructure, int initialCount, {String parentPrefix = ''}) { // Add main group fields/values for (int groupIndex = 0; groupIndex < initialCount; groupIndex++) { - _addGroupControllers(groupStructure, groupIndex, parentPrefix: parentPrefix); + _addGroupControllers(groupStructure, groupIndex, parentPrefix: parentPrefix.isEmpty ? null : parentPrefix); // Recursively handle subgroups if (groupStructure.subGroups != null && groupStructure.subGroups!.isNotEmpty) { - for (final (FormGroupStructure subGroup, int subgroupInitialCount) in groupStructure.subGroups!) { - final String subgroupPrefix = parentPrefix.isEmpty - ? standeredGroupFormat(groupStructure.id, groupIndex, subGroup.id) - : '$parentPrefix->${standeredGroupFormat(groupStructure.id, groupIndex, subGroup.id)}'; // Add to parentPrefix only once + for (final (int subgroupInitialCount, FormGroupStructure subGroup) in groupStructure.subGroups!) { + final String subgroupPrefix = + parentPrefix.isEmpty ? standeredGroupFormat(groupStructure.id, groupIndex, subGroup.id) : standeredGroupFormat(parentPrefix, groupIndex, subGroup.id); // Add to parentPrefix only once // Initialize subgroup controllers recursively for (int subIndex = 0; subIndex < subgroupInitialCount; subIndex++) { - _initializeGroupControllers(subGroup, subgroupInitialCount, parentPrefix: subgroupPrefix); + _initializeGroupControllersRecursively(subGroup, subgroupInitialCount, parentPrefix: subgroupPrefix); } } } } } - /// Validate subgroups recursively - void _validateSubGroups(FormGroupStructure groupStructure) { - groupStructure.subGroups?.forEach((subGroupTuple) { - final subGroup = subGroupTuple.$1; - assert(subGroup.fields.isNotEmpty, '${subGroup.id}: Subgroup fields should NOT be empty.'); + void initializeFormGroup(FormGroupStructure groupStructure, {int initialCount = 1}) { + assert(groupStructure.fields.isNotEmpty, '${groupStructure.id}: Group fields should NOT be empty.'); - // Recursively validate subgroups of subgroups - if (subGroup.subGroups != null) { - _validateSubGroups(subGroup); - } - }); + // Validate subgroups (if any) + _validateSubGroupsRecursively(groupStructure); + + // Add structure to registry + _formGroups.add(groupStructure); + + // Initialize the group instances + _initializeGroupControllersRecursively(groupStructure, initialCount); } // void initializeFormGroup(FormGroupStructure groupStructure, {int initialCount = 1}) { @@ -508,8 +508,8 @@ class AstromicFormController extends FormController { // Recursively count instances in subgroups if (groupStructure.subGroups != null) { - for ((FormGroupStructure, int) subGroup in groupStructure.subGroups!) { - instanceCount += countInstancesRecursively(subGroup.$1, controllersKeys); + for ((int, FormGroupStructure) subGroup in groupStructure.subGroups!) { + instanceCount += countInstancesRecursively(subGroup.$2, controllersKeys); } } @@ -551,7 +551,7 @@ class AstromicFormController extends FormController { // get the subGroups List subValues = []; if (groupStructure.subGroups != null && groupStructure.subGroups!.isNotEmpty) { - subValues = groupStructure.subGroups!.map(((FormGroupStructure, int) s) => getFormGroupValue(s.$1.id, isSubGroup: true)).nonNulls.toList(); + subValues = groupStructure.subGroups!.map(((int, FormGroupStructure) s) => getFormGroupValue(s.$2.id, isSubGroup: true)).nonNulls.toList(); } print('subValues: $subValues'); @@ -639,7 +639,7 @@ class AstromicFormController extends FormController { //!SECTION //SECTION - Helper Methods - String standeredGroupFormat(String groupID, int groupIndex, String? fieldID) => '$groupID-#$groupIndex-${fieldID ?? ""}'; + String standeredGroupFormat(String groupID, int groupIndex, String? secondaryID) => '$groupID-#$groupIndex-${secondaryID ?? ""}'; void _addInitialControllers(Map? initialValues) { if (initialValues != null) { diff --git a/lib/src/form/src/models/form_group_structure.model.dart b/lib/src/form/src/models/form_group_structure.model.dart index 9827edc..2b825c4 100644 --- a/lib/src/form/src/models/form_group_structure.model.dart +++ b/lib/src/form/src/models/form_group_structure.model.dart @@ -6,7 +6,7 @@ class FormGroupStructure { final String id; final List fields; final List? values; - final List<(FormGroupStructure structure, int initialCount)>? subGroups; + final List<(int initialCount, FormGroupStructure structure)>? subGroups; FormGroupStructure({ required this.id, required this.fields, @@ -18,7 +18,7 @@ class FormGroupStructure { String? id, List? fields, List? values, - List<(FormGroupStructure structure, int initialCount)>? subGroups, + List<(int initialCount, FormGroupStructure structure)>? subGroups, }) { return FormGroupStructure( id: id ?? this.id, @@ -33,7 +33,7 @@ class FormGroupStructure { 'id': id, 'fields': fields, 'values': values, - 'subGroups': subGroups?.map(((FormGroupStructure structure, int initialCount) x) => {'structure': x.$1.toMap(), 'initialCount': x.$2}).toList(), + 'subGroups': subGroups?.map(((int initialCount, FormGroupStructure structure) x) => {'structure': x.$2.toMap(), 'initialCount': x.$1}).toList(), }; } @@ -43,7 +43,7 @@ class FormGroupStructure { fields: List.from(map['fields'] as List), values: map['values'] != null ? List.from(map['values'] as List) : null, subGroups: map['subGroups'] != null - ? (map['subGroups'] as List>).map((Map map) => (FormGroupStructure.fromMap(map['structure']), int.tryParse(map['initialCount']) ?? 0)).toList() + ? (map['subGroups'] as List>).map((Map map) => (int.tryParse(map['initialCount']) ?? 0, FormGroupStructure.fromMap(map['structure']))).toList() : null, ); }