Update form key to be a final field and add validators for email and password

- Make the form key a final field in the AuthCubit class
- Add validators for email and password fields in the AuthCubit class
- Update the LoginForm widget to use the form key from AuthCubit class
- Add validators for email and password fields in the LoginForm widget
This commit is contained in:
Mohammad Salameh
2024-04-16 16:44:17 +03:00
parent fd03d29e93
commit 6266780258
2 changed files with 76 additions and 46 deletions
+53 -2
View File
@@ -20,8 +20,9 @@ class AuthCubit extends Cubit<AuthState> {
static AuthCubit get(context) => BlocProvider.of(context);
TextEditingController emailController = TextEditingController();
TextEditingController passwordController = TextEditingController();
final TextEditingController emailController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
final loginFormKey = GlobalKey<FormState>();
bool isPasswordVisible = false;
static GlobalKey<FormState> formKey = GlobalKey<FormState>();
@@ -41,6 +42,56 @@ class AuthCubit extends Cubit<AuthState> {
static UserModel? user;
static Token token = Token.emptyConstructor();
/////////////////////////////////////VALIDATORS/////////////////////////////////////
String? passwordValidator(String? value) {
if (value != null) {
if (value.isNotEmpty) {
if (value.length >= 6) {
return null;
//TODO uncomment this code when the password validation is needed
// if (RegExp(
// r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$')
// .hasMatch(value)) {
// return null;
// } else {
// return 'Password must contain at least one uppercase letter, one lowercase letter, one number and one special character';
// }
} else {
return 'Password must be at least 6 characters';
}
} else {
return 'Please enter your password';
}
}
return null;
}
String? emailAddressValidator(String? value) {
if (value != null && value.isNotEmpty && value != "") {
if (checkValidityOfEmail(value)) {
if (loginFormKey.currentState != null) {
loginFormKey.currentState!.save();
}
return null;
} else {
return 'Please enter a valid email';
}
} else {
return 'Email address is required';
}
}
bool checkValidityOfEmail(String? email) {
if (email != null) {
return RegExp(
r"^[a-zA-Z0-9]+([.!#$%&'*+/=?^_`{|}~-]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([.-]?[a-zA-Z0-9]+)*\.[a-zA-Z0-9]{2,}$")
.hasMatch(email);
} else {
return false;
}
}
/////////////////////////////////////API CALLS/////////////////////////////////////
login() async {
emit(AuthLoginLoading());