marco.pms.mobile/lib/helpers/widgets/my_form_validator.dart
Vaibhav Surve 99902e743c Flutter application
- Created generated_plugin_registrant.cc and generated_plugin_registrant.h to manage plugin registration.
- Added generated_plugins.cmake for plugin configuration in CMake.
- Implemented CMakeLists.txt for the Windows runner, defining build settings and dependencies.
- Created Runner.rc for application resources including versioning and icons.
- Developed flutter_window.cpp and flutter_window.h to manage the Flutter window lifecycle.
- Implemented main.cpp as the entry point for the Windows application.
- Added resource.h for resource definitions.
- Included app icon in resources.
- Created runner.exe.manifest for application settings.
- Developed utils.cpp and utils.h for console management and command line argument handling.
- Implemented win32_window.cpp and win32_window.h for high DPI-aware window management.
2025-04-17 12:30:38 +05:30

114 lines
3.1 KiB
Dart

import 'package:marco/helpers/widgets/my_field_validator.dart';
import 'package:flutter/material.dart';
import 'package:get/get_utils/get_utils.dart';
class MyFormValidator {
Map<String, dynamic> errors = {};
Map<String, dynamic> remainingError = {};
GlobalKey<FormState> formKey = GlobalKey();
bool consumeError = true;
final Map<String, dynamic> _validators = {};
final Map<String, TextEditingController> _controllers = {};
final Map<String, dynamic> _data = {};
void addField<T>(String name,
{bool required = false, List<MyFieldValidatorRule<T>> validators = const [], String? label, TextEditingController? controller}) {
_validators[name] = _createValidation<T>(name, required: required, validators: validators, label: label);
if (controller != null) _controllers[name] = controller;
}
MyFieldValidator<T>? getValidation<T>(String name) => _validators[name] != null ? _validators[name] as MyFieldValidator<T> : null;
TextEditingController? getController(String name) => _controllers[name];
MyFieldValidator<T> _createValidation<T>(String name, {bool required = false, List<MyFieldValidatorRule<T>> validators = const [], String? label}) {
return (T? value) {
label ??= name.capitalize;
String? error = getError(name);
if (error != null) {
return error;
}
if (required && (value == null || (value.toString().isEmpty))) {
return "$label is required";
}
for (MyFieldValidatorRule validator in validators) {
String? validationError = validator.validate(value, required, getData());
if (validationError != null) {
return validationError;
}
}
return null;
};
}
String? getError(String name) {
if (errors.containsKey(name)) {
dynamic error = errors[name];
if (error is List && error.isNotEmpty) {
String errorText = error[0].toString();
if (consumeError) {
remainingError.remove(name);
}
return errorText;
} else {
String errorText = error.toString();
if (consumeError) {
remainingError.remove(name);
}
return errorText;
}
}
return null;
}
bool validateForm({bool clear = false, bool consumeError = true}) {
if (clear) {
errors.clear();
remainingError.clear();
}
this.consumeError = consumeError;
return formKey.currentState?.validate() ?? false;
}
ValueChanged<T> onChanged<T>(String key) {
return (T value) {
_data[key] = value;
};
}
Map<String, dynamic> getData() {
var map = {
..._data,
};
for (var key in _controllers.keys) {
if (_controllers[key]?.text != null) {
map[key] = _controllers[key]!.text;
}
}
return map;
}
void resetForm() {
formKey.currentState?.reset();
}
void clearErrors() {
errors.clear();
}
void addError(String key, dynamic error) {
errors[key] = error;
}
void addErrors(Map<String, dynamic> errors) {
errors.forEach((key, value) {
this.errors[key] = value;
});
}
}