diff --git a/lib/src/form/src/controller.dart b/lib/src/form/src/controller.dart index 2c50cc1..940f0c6 100644 --- a/lib/src/form/src/controller.dart +++ b/lib/src/form/src/controller.dart @@ -200,6 +200,40 @@ class AstromicFormController { return allValid; } + /// Automatically validates every instance of a specific [groupId]. + /// + /// It looks up the [AstromicFormGroupDefinition], finds all active UUIDs in the manifest, + /// and triggers [validateOnly] for every field defined in the schema. + /// + /// Returns `true` if all instances are valid. + bool validateGroup(String groupId, {String? parentUuid}) { + // 1. Get the definition to know which fields exist + final def = _groupDefs[groupId]; + if (def == null) { + throw Exception("Validation Error: Group '$groupId' is not registered."); + } + + // 2. Get all active UUIDs for this group/parent context + final manifest = getManifest(groupId, parentUuid); + final List activeUuids = manifest.value; + + if (activeUuids.isEmpty) return true; // Empty groups are technically valid unless a "Min Items" rule exists + + // 3. Collect every field ID for every instance + final List idsToValidate = []; + for (final uuid in activeUuids) { + for (final fieldName in def.schema.keys) { + idsToValidate.add('${groupId}_${fieldName}_$uuid'); + } + + // OPTIONAL: If you want to auto-validate nested subgroups, + // you could recursively call validateGroup for each id in def.subGroups here. + } + + // 4. Delegate to the core validation logic + return validateOnly(idsToValidate); + } + /// Exports the data of a specific group as a list of maps, including all nested subgroups. List> getGroupData(String groupId, {String? parentUuid}) { final manifest = getManifest(groupId, parentUuid); @@ -210,8 +244,8 @@ class AstromicFormController { final Map row = {}; for (var fieldName in def.schema.keys) { - final fieldId = "${groupId}_${fieldName}_$uuid"; - row[fieldName] = get(fieldId); + final fieldId = '${groupId}_${fieldName}_$uuid'; + row[fieldName] = get(fieldId); } for (var subGroupId in def.subGroups) { diff --git a/lib/src/form/src/helpers/form_group_helper.dart b/lib/src/form/src/helpers/form_group_helper.dart index 09ce5a9..dfd1595 100644 --- a/lib/src/form/src/helpers/form_group_helper.dart +++ b/lib/src/form/src/helpers/form_group_helper.dart @@ -47,22 +47,8 @@ class AstromicGroupHandler { } /// Performs validation on every registered field within all instances of this specific group. - /// - /// Returns `true` if all fields pass their respective validation contracts. bool validate() { - final def = _ctrl.groupDefs[_groupId]; - if (def == null) return true; - - final activeUuids = _ids.value; - final List idsToValidate = []; - - for (var uuid in activeUuids) { - for (var fieldName in def.schema.keys) { - idsToValidate.add('${_groupId}_${fieldName}_$uuid'); - } - } - - return _ctrl.validateOnly(idsToValidate); + return _ctrl.validateGroup(_groupId, parentUuid: _parentUuid); } }