This commit is contained in:
2025-03-10 19:58:26 +02:00
parent 968d57a8b1
commit 25affbbda1
2 changed files with 36 additions and 36 deletions

View File

@@ -19,24 +19,24 @@ class AstromicFormController extends FormController {
final Map<String, AstromicFieldState> fieldStates = <String, AstromicFieldState>{};
final Map<String, String?> fieldMessages = <String, String?>{};
final Map<String, dynamic> _hostedValues = <String, dynamic>{};
final Map<String, Map<String, String>>? streamedErrorMaps; // fieldId: {errorCode: errorMessage}
final Stream<List<String>>? errorStream;
// final Map<String, Map<String, String>>? streamedErrorMaps; // fieldId: {errorCode: errorMessage}
final Stream<List<(String internalCode,String? message)>>? errorStream;
// State Stream Variables
static final StreamController<(String, AstromicFieldState)> _stateStreamController = StreamController<(String id, AstromicFieldState)>.broadcast();
final Stream<(String id, AstromicFieldState)> stateStream = _stateStreamController.stream;
AstromicFormController({
Map<String, (String initialText, bool initialObscurity)>? extraControllers,
this.streamedErrorMaps,
// this.streamedErrorMaps,
this.errorStream,
}) : super(controllers: extraControllers) {
// Add states and messages based on initial controller.
_addInitialControllers();
// Listen on the error stream for values and push to the corresponding field state.
if (errorStream != null) {
_handleErrorStream();
}
// if (errorStream != null) {
// _handleErrorStream();
// }
}
/// Get the field state and message of a specific field using it's ID.
@@ -124,23 +124,23 @@ class AstromicFormController extends FormController {
<MapEntry<String, String?>>{});
}
_handleErrorStream() {
errorStream!.distinct().listen((List<String> errorCodes) {
if (streamedErrorMaps != null && streamedErrorMaps!.isNotEmpty) {
for (String errorMapId in streamedErrorMaps!.keys.toList()) {
if (super.controllers != null && super.controllers!.containsKey(errorMapId)) {
if (streamedErrorMaps![errorMapId] != null &&
streamedErrorMaps![errorMapId]!.isNotEmpty &&
streamedErrorMaps![errorMapId]!.keys.toList().where((String k) => errorCodes.contains(k)).toList().isNotEmpty) {
for (String eC in streamedErrorMaps![errorMapId]!.keys.toList().where((String k) => errorCodes.contains(k)).toList()) {
String? m = streamedErrorMaps![errorMapId]![eC];
setState(errorMapId, AstromicFieldState.withError, message: m ?? 'Error Message was not set!');
}
}
}
}
}
});
}
// _handleErrorStream() {
// errorStream!.distinct().listen((List<String> errorCodes) {
// if (streamedErrorMaps != null && streamedErrorMaps!.isNotEmpty) {
// for (String errorMapId in streamedErrorMaps!.keys.toList()) {
// if (super.controllers != null && super.controllers!.containsKey(errorMapId)) {
// if (streamedErrorMaps![errorMapId] != null &&
// streamedErrorMaps![errorMapId]!.isNotEmpty &&
// streamedErrorMaps![errorMapId]!.keys.toList().where((String k) => errorCodes.contains(k)).toList().isNotEmpty) {
// for (String eC in streamedErrorMaps![errorMapId]!.keys.toList().where((String k) => errorCodes.contains(k)).toList()) {
// String? m = streamedErrorMaps![errorMapId]![eC];
// setState(errorMapId, AstromicFieldState.withError, message: m ?? 'Error Message was not set!');
// }
// }
// }
// }
// }
// });
// }
//!SECTION
}

View File

@@ -24,6 +24,7 @@ class AstromicFormField<T> extends StatefulWidget {
final AstromicFieldConfiguration? configuration;
final List<FormControllerValidator>? validators;
final bool? resetMessageOnChange;
final Map<String, String?>? errorsCatcher;
//
final AstromicFieldStyle Function(bool isEnabled, bool isFocused, AstromicFieldState state)? style;
//
@@ -50,6 +51,7 @@ class AstromicFormField<T> extends StatefulWidget {
this.configuration,
this.validators,
this.resetMessageOnChange,
this.errorsCatcher,
this.style,
this.hint,
this.prefixWidget,
@@ -69,6 +71,7 @@ class AstromicFormField<T> extends StatefulWidget {
this.configuration,
this.validators,
this.resetMessageOnChange,
this.errorsCatcher,
this.style,
this.hint,
this.prefixWidget,
@@ -124,19 +127,16 @@ class _AstromicFormFieldState<T> extends State<AstromicFormField<T>> {
// Listen to the error stream for updated errors and set the field's state accordingly...
if (widget.formController.errorStream != null) {
widget.formController.errorStream!.listen((List<String> errorCodes) {
if (widget.formController.streamedErrorMaps != null) {
widget.formController.errorStream!.listen((List<(String internalCode, String? message)> errorCodes) {
if (widget.errorsCatcher != null) {
// fieldId: {errorCode: errorMessage}
for (String cID in widget.formController.streamedErrorMaps!.keys.toList()) {
if (cID == widget.formID && widget.formController.streamedErrorMaps![cID] != null) {
for (MapEntry<String, String> errMapItem in widget.formController.streamedErrorMaps![cID]!.entries) {
if (errorCodes.map((String x) => x.toString()).contains(errMapItem.key.toString())) {
if (mounted) {
setState(() {
_setFieldErrorState(widget.formID, errMapItem.value);
});
}
}
for (String toBeCaughtInternalErrorCode in widget.errorsCatcher!.keys.toList()) {
if (errorCodes.map(((String, String?) x) => x.$1).contains(toBeCaughtInternalErrorCode)) {
if (mounted) {
setState(() {
_setFieldErrorState(
widget.formID, errorCodes.where(((String, String?) c) => c.$1 == toBeCaughtInternalErrorCode).first.$2 ?? widget.errorsCatcher![toBeCaughtInternalErrorCode] ?? 'Undefined Error Message');
});
}
}
}