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

View File

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