diff --git a/lib/src/form/src/controller.dart b/lib/src/form/src/controller.dart index ae18f60..1dda67d 100644 --- a/lib/src/form/src/controller.dart +++ b/lib/src/form/src/controller.dart @@ -468,32 +468,63 @@ class AstromicFormController extends FormController { } } - bool validateGroup(String groupID, {Map? parents}) { + (List fields, List values) _checkSubgroupsToValidate(String groupID, {Map? parents}) { + FormGroupStructure? groupStructure = getGroupStructure(groupID); + String? prefix; + if (parents != null) { + prefix = parents.entries.map((MapEntry parentEntry) => standeredGroupFormat(parentEntry.key, parentEntry.value.toString(), null)).join('-'); + } + // + // + List fieldsToValidate = []; + List valuesToValidate = []; + for ((int, FormGroupStructure) sg in (groupStructure?.subGroups ?? <(int, FormGroupStructure)>[])) { + FormGroupStructure? subgroupStructure = getGroupStructure(sg.$2.id); + int sgInstances = getInstanceCount(sg.$2.id, parents: {...?parents}); + for (int i = 0; i < sgInstances; i++) { + _checkSubgroupsToValidate( + sg.$2.id, + parents: { + ...?parents, + ...{sg.$2.id: i} + }, + ); + for (String fieldID in subgroupStructure!.fields) { + fieldsToValidate.add(standeredGroupFormat((prefix != null ? '$prefix-' : '') + sg.$2.id, i.toString(), fieldID)); + } + for (String valueID in (subgroupStructure.values ?? [])) { + valuesToValidate.add(standeredGroupFormat((prefix != null ? '$prefix-' : '') + sg.$2.id, i.toString(), valueID)); + } + } + } + return (fieldsToValidate, valuesToValidate); + } + + /// Validate the group with ID. + bool validateGroup(String groupID) { // Get tha main targeted group's structure FormGroupStructure? groupStructure = getGroupStructure(groupID); assert(groupStructure != null, 'The Group $groupID doesn\'t seem to be found, are you sure you initialized it?'); - // Get the prefix for subgroups if exists - String? prefix; - if (isASubGroup(groupID) && parents != null) { - prefix = parents.entries.map((MapEntry parentEntry) => standeredGroupFormat(parentEntry.key, parentEntry.value.toString(), null)).join('-'); - } - // Get current instances of this group and the new index. - int currentGroupInstances = getInstanceCount(groupID, parents: parents); + int currentGroupInstances = getInstanceCount(groupID); List fieldsToValidate = []; List valuesToValidate = []; + for (int i = 0; i < currentGroupInstances; i++) { for (String fieldID in groupStructure!.fields) { - fieldsToValidate.add((prefix != null ? '$prefix-' : '') + standeredGroupFormat(groupID, i.toString(), fieldID)); + fieldsToValidate.add(standeredGroupFormat(groupID, i.toString(), fieldID)); } for (String valueID in (groupStructure.values ?? [])) { - valuesToValidate.add((prefix != null ? '$prefix-' : '') + standeredGroupFormat(groupID, i.toString(), valueID)); + valuesToValidate.add(standeredGroupFormat(groupID, i.toString(), valueID)); } - } - // + // Loop through the subgroups and get their fields and values + (List fields, List values) sgRet = _checkSubgroupsToValidate(groupID, parents: {groupID: i}); + fieldsToValidate.addAll(sgRet.$1); + valuesToValidate.addAll(sgRet.$2); + } // return validateOnly(fieldsToValidate) && validateValues(valuesToValidate); } //!SECTION