From d8ef3034a2dad35cae4d6a7657949720d30ca107 Mon Sep 17 00:00:00 2001
From: Naresh Pratista <2141720057@student.polinema.ac.id>
Date: Mon, 2 Dec 2024 14:12:42 +0700
Subject: [PATCH] Extracted login validation logic from _validateLogin method
 and moved it to alidator callback in TextFormField widget.

---
 .../auth/screens/signin/signin_screen.dart    | 71 ++++++++++++-------
 1 file changed, 45 insertions(+), 26 deletions(-)

diff --git a/lib/features/auth/screens/signin/signin_screen.dart b/lib/features/auth/screens/signin/signin_screen.dart
index 8c33f79..c1e3d17 100644
--- a/lib/features/auth/screens/signin/signin_screen.dart
+++ b/lib/features/auth/screens/signin/signin_screen.dart
@@ -115,22 +115,6 @@ class _SigninScreenState extends State<SigninScreen> {
     CustomSnackBar.show(context, message: message, isError: true);
   }
 
-  String? _validateLogin(String? value) {
-    if (value == null || value.isEmpty) {
-      return 'Login credential cannot be empty';
-    }
-
-    value = value.trim();
-
-    if (value.contains('@')) {
-      return context.read<ValidatorProvider>().emailValidator(value);
-    } else if (RegExp(r'^\d{ 10}$').hasMatch(value)) {
-      return null; // Valid NISN
-    } else {
-      return 'Please enter a valid email or NISN (10 digits)';
-    }
-  }
-
   @override
   Widget build(BuildContext context) {
     return GestureDetector(
@@ -190,20 +174,50 @@ class _SigninScreenState extends State<SigninScreen> {
                             labelText: 'Email or NISN',
                             hintText: 'Enter Email or NISN Number',
                             keyboardType: TextInputType.emailAddress,
-                            validator: _validateLogin,
+                            validator: (value) {
+                              validatorProvider.validateField(
+                                  'login', value?.trim(), validator: (val) {
+                                if (val == null || val.isEmpty) {
+                                  return 'Login credential cannot be empty';
+                                }
+
+                                val = val.trim();
+
+                                if (val.contains('@')) {
+                                  return validatorProvider.emailValidator(val);
+                                } else if (RegExp(r'^\d{10}$').hasMatch(val)) {
+                                  return null; // Valid NISN
+                                } else {
+                                  return 'Please enter a valid email or NISN (10 digits)';
+                                }
+                              });
+                              return validatorProvider.getError('login');
+                            },
                             onChanged: (value) =>
-                                context.read<ValidatorProvider>().validateField(
-                                      'login',
-                                      value?.trim(),
-                                      validator: _validateLogin,
-                                    ),
+                                validatorProvider.validateField(
+                              'login',
+                              value?.trim(),
+                              validator: (val) {
+                                if (val == null || val.isEmpty) {
+                                  return 'Login credential cannot be empty';
+                                }
+
+                                val = val.trim();
+
+                                if (val.contains('@')) {
+                                  return validatorProvider.emailValidator(val);
+                                } else if (RegExp(r'^\d{10}$').hasMatch(val)) {
+                                  return null; // Valid NISN
+                                } else {
+                                  return 'Please enter a valid email or NISN (10 digits)';
+                                }
+                              },
+                            ),
                             onFieldSubmitted: (_) {
                               FocusScope.of(context)
                                   .requestFocus(_passwordFocus);
                             },
-                            errorText: context
-                                .read<ValidatorProvider>()
-                                .getError('login'),
+                            errorText: validatorProvider.getError('login'),
                           ),
                           const SizedBox(height: 14),
                           CustomFieldWidget(
@@ -217,7 +231,12 @@ class _SigninScreenState extends State<SigninScreen> {
                             obscureText:
                                 validatorProvider.isObscure('password'),
                             keyboardType: TextInputType.visiblePassword,
-                            validator: validatorProvider.passwordValidator,
+                            validator: (value) {
+                              validatorProvider.validateField('password', value,
+                                  validator:
+                                      validatorProvider.passwordValidator);
+                              return validatorProvider.getError('password');
+                            },
                             onChanged: (value) =>
                                 validatorProvider.validateField(
                               'password',
