From 7e9ea0134b79bf6442eee7c0594928091b5e0746 Mon Sep 17 00:00:00 2001 From: VE10-Sanjeev Date: Thu, 22 May 2025 12:26:23 +0000 Subject: [PATCH] validation in departments --- lib/Screens/department/departmentDetails.dart | 64 ++++++++++++------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/lib/Screens/department/departmentDetails.dart b/lib/Screens/department/departmentDetails.dart index e887bca..43d57d3 100644 --- a/lib/Screens/department/departmentDetails.dart +++ b/lib/Screens/department/departmentDetails.dart @@ -34,8 +34,19 @@ class DepartmentData extends StatefulWidget { class DepartmentDataState extends State { final ApiService apiService = ApiService(); Map? apiData; - final Map controllers = {}; - Map errorMessages = {}; + + final Map controllers = { + "name": TextEditingController(), + "description": TextEditingController(), + }; + + final Map focusNodes = { + "name": FocusNode(), + "description": FocusNode(), + }; + + final Map errorMessages = {}; + String? selectedName; String? selectedDescription; String? userId; @@ -103,7 +114,6 @@ class DepartmentDataState extends State { } bool validateData() { - errorMessages.clear(); final data = { @@ -111,15 +121,17 @@ class DepartmentDataState extends State { "description": controllers["description"]?.text, }; - final requiredFields = [ - "name", - "description" - ]; + final requiredFields = ["name", "description"]; + bool hasFocused = false; - // Check validation for each field for (String field in requiredFields) { - if (data[field] == null || data[field].toString().trim().isEmpty) { + if (data[field] == null || data[field]!.trim().isEmpty) { errorMessages[field] = "Required"; + + if (!hasFocused) { + focusNodes[field]?.requestFocus(); + hasFocused = true; + } } } @@ -130,12 +142,14 @@ class DepartmentDataState extends State { print("inside submit"); userId = await getUserId(); - setState(() { - // This triggers UI rebuild with error messages - if (validateData()) { - postDepartmentData(); - } - }); + final isValid = validateData(); + setState(() {}); // <-- triggers UI update to show errors + + if (isValid) { + // Proceed to submit data + postDepartmentData(); + print("Submitting data: ${controllers["name"]?.text}"); + } final departmentData1 = departmentDetails(); print("submit data - $departmentData1"); @@ -147,7 +161,7 @@ class DepartmentDataState extends State { final departmentData = departmentDetails(); - print("initally value of the Department - $departmentData"); + print("initially value of the Department - $departmentData"); final String apiUrldata; @@ -160,7 +174,7 @@ class DepartmentDataState extends State { } else { print("for add Department id - null"); apiUrldata = '$apiUrl/api/createDepartment'; - print("called apiurl - $apiUrldata"); + print("called apiUrl - $apiUrldata"); departmentData["created_by"] = userId; } print("recently Department data - $departmentData"); @@ -223,7 +237,7 @@ class DepartmentDataState extends State { Row( children: [ Text( - 'Create Department', + (departmentDataId != null) ? 'Edit Department' : 'Create Department', style: GoogleFonts.poppins(fontSize: 15, color: Colors.black), ), const Spacer(), @@ -252,12 +266,14 @@ class DepartmentDataState extends State { isDesktop: widget.isDesktop, color: Colors.transparent, child: SizedBox( - height: 40, + height: 45, child: TextField( controller: controllers["name"], + focusNode: focusNodes["name"], style: const TextStyle(fontSize: 12), - decoration: const InputDecoration( + decoration: InputDecoration( labelText: "Department Name", + errorText: errorMessages["name"], labelStyle: TextStyle(fontSize: 11, color: Colors.grey), floatingLabelBehavior: FloatingLabelBehavior.never, border: InputBorder.none, @@ -292,15 +308,17 @@ class DepartmentDataState extends State { height: 100, child: TextField( controller: controllers["description"], - style: const TextStyle(fontSize: 11), + focusNode: focusNodes["description"], + style: const TextStyle(fontSize: 12), maxLines: null, // Allow infinite lines (textarea behavior) expands: true, // Expands to fill the parent height - decoration: const InputDecoration( + decoration: InputDecoration( labelText: "Description", + errorText: errorMessages["description"], labelStyle: TextStyle(fontSize: 11, color: Colors.grey), floatingLabelBehavior: FloatingLabelBehavior.never, border: InputBorder.none, - contentPadding: EdgeInsets.symmetric(vertical: 16, horizontal: 8), + contentPadding: EdgeInsets.symmetric(vertical: 16), ), ), ),