This commit is contained in:
2025-04-08 15:14:07 +02:00
parent 9bd62ae5df
commit 48280e3ff3

View File

@@ -287,33 +287,79 @@ class AstromicFormController extends FormController {
} }
/// Initialize the formGroup Structure. /// Initialize the formGroup Structure.
// void initializeFormGroup(FormGroupStructure groupStructure, {int initialCount = 1}) {
// assert(groupStructure.fields.isNotEmpty, '${groupStructure.id}: Group fields should NOT be empty.');
// // Validate subgroups (if any)
// groupStructure.subGroups?.map(((FormGroupStructure, int) a) => a.$1).forEach(((FormGroupStructure subGroup) {
// assert(subGroup.fields.isNotEmpty, '${subGroup.id}: Subgroup fields should NOT be empty.');
// }));
// // Add structure to registry
// _formGroups.add(groupStructure);
// for (int groupIndex = 0; groupIndex < initialCount; groupIndex++) {
// // Add main group fields/values
// _addGroupControllers(groupStructure, groupIndex);
// // Add subgroup fields/values
// if (groupStructure.subGroups != null && groupStructure.subGroups!.isNotEmpty) {
// for (final (FormGroupStructure subGroup, int subgroupInitialCount) in groupStructure.subGroups!) {
// for (int subIndex = 0; subIndex < subgroupInitialCount; subIndex++) {
// final String nestedID = standeredGroupFormat(groupStructure.id, groupIndex, subGroup.id);
// _addGroupControllers(subGroup, subIndex, parentPrefix: nestedID);
// }
// }
// }
// }
// }
void initializeFormGroup(FormGroupStructure groupStructure, {int initialCount = 1}) { void initializeFormGroup(FormGroupStructure groupStructure, {int initialCount = 1}) {
assert(groupStructure.fields.isNotEmpty, '${groupStructure.id}: Group fields should NOT be empty.'); assert(groupStructure.fields.isNotEmpty, '${groupStructure.id}: Group fields should NOT be empty.');
// Validate subgroups (if any) // Validate subgroups (if any)
groupStructure.subGroups?.map(((FormGroupStructure, int) a) => a.$1).forEach(((FormGroupStructure subGroup) { _validateSubGroups(groupStructure);
assert(subGroup.fields.isNotEmpty, '${subGroup.id}: Subgroup fields should NOT be empty.');
}));
// Add structure to registry // Add structure to registry
_formGroups.add(groupStructure); _formGroups.add(groupStructure);
for (int groupIndex = 0; groupIndex < initialCount; groupIndex++) { // Initialize the group instances
// Add main group fields/values _initializeGroupControllers(groupStructure, initialCount);
_addGroupControllers(groupStructure, groupIndex); }
// Add subgroup fields/values /// Recursively initialize controllers for the group and its subgroups
void _initializeGroupControllers(FormGroupStructure groupStructure, int initialCount, {String parentPrefix = ''}) {
// Add main group fields/values
for (int groupIndex = 0; groupIndex < initialCount; groupIndex++) {
_addGroupControllers(groupStructure, groupIndex, parentPrefix: parentPrefix);
// Recursively handle subgroups
if (groupStructure.subGroups != null && groupStructure.subGroups!.isNotEmpty) { if (groupStructure.subGroups != null && groupStructure.subGroups!.isNotEmpty) {
for (final (FormGroupStructure subGroup, int subgroupInitialCount) in groupStructure.subGroups!) { for (final (FormGroupStructure subGroup, int subgroupInitialCount) in groupStructure.subGroups!) {
final String nestedPrefix = '$parentPrefix${standeredGroupFormat(groupStructure.id, groupIndex, subGroup.id)}';
// Initialize subgroup controllers recursively
for (int subIndex = 0; subIndex < subgroupInitialCount; subIndex++) { for (int subIndex = 0; subIndex < subgroupInitialCount; subIndex++) {
final String nestedID = standeredGroupFormat(groupStructure.id, groupIndex, subGroup.id); _initializeGroupControllers(subGroup, subgroupInitialCount, parentPrefix: nestedPrefix);
_addGroupControllers(subGroup, subIndex, parentPrefix: nestedID);
} }
} }
} }
} }
} }
/// 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.');
// Recursively validate subgroups of subgroups
if (subGroup.subGroups != null) {
_validateSubGroups(subGroup);
}
});
}
// void initializeFormGroup(FormGroupStructure groupStructure, {int initialCount = 1}) { // void initializeFormGroup(FormGroupStructure groupStructure, {int initialCount = 1}) {
// assert(groupStructure.fields.isNotEmpty, '$groupStructure: Group Fields should NOT be empty.'); // assert(groupStructure.fields.isNotEmpty, '$groupStructure: Group Fields should NOT be empty.');
// if (groupStructure.subGroups != null && groupStructure.subGroups!.isNotEmpty) { // if (groupStructure.subGroups != null && groupStructure.subGroups!.isNotEmpty) {