[FEAT] Added group validation.
This commit is contained in:
@@ -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<String> 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<String> 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<Map<String, dynamic>> getGroupData(String groupId, {String? parentUuid}) {
|
||||
final manifest = getManifest(groupId, parentUuid);
|
||||
@@ -210,8 +244,8 @@ class AstromicFormController {
|
||||
final Map<String, dynamic> 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<dynamic>(fieldId);
|
||||
}
|
||||
|
||||
for (var subGroupId in def.subGroups) {
|
||||
|
||||
@@ -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<String> 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user