From 25affbbda1eddcb07b74e3f8310a3ccedd9cd763 Mon Sep 17 00:00:00 2001 From: "Michael W. Aziz" Date: Mon, 10 Mar 2025 19:58:26 +0200 Subject: [PATCH] [0.1.2] --- lib/src/form/src/controller.dart | 48 ++++++++++++++++---------------- lib/src/form/src/form_field.dart | 24 ++++++++-------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/lib/src/form/src/controller.dart b/lib/src/form/src/controller.dart index 68aed02..c011fab 100644 --- a/lib/src/form/src/controller.dart +++ b/lib/src/form/src/controller.dart @@ -19,24 +19,24 @@ class AstromicFormController extends FormController { final Map fieldStates = {}; final Map fieldMessages = {}; final Map _hostedValues = {}; - final Map>? streamedErrorMaps; // fieldId: {errorCode: errorMessage} - final Stream>? errorStream; + // final Map>? streamedErrorMaps; // fieldId: {errorCode: errorMessage} + final Stream>? 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? 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 { >{}); } - _handleErrorStream() { - errorStream!.distinct().listen((List 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 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 } diff --git a/lib/src/form/src/form_field.dart b/lib/src/form/src/form_field.dart index 5979a98..cacf52e 100644 --- a/lib/src/form/src/form_field.dart +++ b/lib/src/form/src/form_field.dart @@ -24,6 +24,7 @@ class AstromicFormField extends StatefulWidget { final AstromicFieldConfiguration? configuration; final List? validators; final bool? resetMessageOnChange; + final Map? errorsCatcher; // final AstromicFieldStyle Function(bool isEnabled, bool isFocused, AstromicFieldState state)? style; // @@ -50,6 +51,7 @@ class AstromicFormField extends StatefulWidget { this.configuration, this.validators, this.resetMessageOnChange, + this.errorsCatcher, this.style, this.hint, this.prefixWidget, @@ -69,6 +71,7 @@ class AstromicFormField extends StatefulWidget { this.configuration, this.validators, this.resetMessageOnChange, + this.errorsCatcher, this.style, this.hint, this.prefixWidget, @@ -124,19 +127,16 @@ class _AstromicFormFieldState extends State> { // 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 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 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'); + }); } } }