From 157b329de1cdee9f605969916d8eea5e16bd6c4f Mon Sep 17 00:00:00 2001 From: VE10-Sanjeev Date: Thu, 22 May 2025 13:01:12 +0000 Subject: [PATCH] validation in departments --- lib/Screens/department/departmentDetails.dart | 71 +++++++++++-------- 1 file changed, 40 insertions(+), 31 deletions(-) diff --git a/lib/Screens/department/departmentDetails.dart b/lib/Screens/department/departmentDetails.dart index 43d57d3..078e395 100644 --- a/lib/Screens/department/departmentDetails.dart +++ b/lib/Screens/department/departmentDetails.dart @@ -35,17 +35,13 @@ class DepartmentDataState extends State { final ApiService apiService = ApiService(); Map? apiData; - final Map controllers = { - "name": TextEditingController(), - "description": TextEditingController(), - }; - final Map focusNodes = { "name": FocusNode(), "description": FocusNode(), }; - final Map errorMessages = {}; + final Map controllers = {}; + Map errorMessages = {}; String? selectedName; String? selectedDescription; @@ -91,6 +87,14 @@ class DepartmentDataState extends State { }); } + @override + void dispose() { + for (var controller in controllers.values) { + controller.dispose(); + } + super.dispose(); + } + void updateDepartmentDetails() { print("Inside Update Function - ${widget.departmentData}"); @@ -124,6 +128,7 @@ class DepartmentDataState extends State { final requiredFields = ["name", "description"]; bool hasFocused = false; + // Check validation for each field for (String field in requiredFields) { if (data[field] == null || data[field]!.trim().isEmpty) { errorMessages[field] = "Required"; @@ -139,21 +144,17 @@ class DepartmentDataState extends State { } Future handleSubmit() async { - print("inside submit"); userId = await getUserId(); - final isValid = validateData(); - setState(() {}); // <-- triggers UI update to show errors - - if (isValid) { - // Proceed to submit data - postDepartmentData(); - print("Submitting data: ${controllers["name"]?.text}"); - } + setState(() { + // This triggers UI rebuild with error messages + if (validateData()) { + postDepartmentData(); + } + }); final departmentData1 = departmentDetails(); print("submit data - $departmentData1"); - } Future postDepartmentData({int isActive = 1}) async { @@ -260,28 +261,32 @@ class DepartmentDataState extends State { color: Color(0xFF575A74)), ), SizedBox(height: 5), - CustomTextFieldForexWrapper( isFocused: false, isDesktop: widget.isDesktop, color: Colors.transparent, child: SizedBox( - height: 45, + height: 40, child: TextField( controller: controllers["name"], focusNode: focusNodes["name"], style: const TextStyle(fontSize: 12), - decoration: InputDecoration( - labelText: "Department Name", - errorText: errorMessages["name"], + decoration: const InputDecoration( + labelText: "Name", labelStyle: TextStyle(fontSize: 11, color: Colors.grey), floatingLabelBehavior: FloatingLabelBehavior.never, border: InputBorder.none, contentPadding: EdgeInsets.symmetric(vertical: 16), ), - // validator: (val) => val == null || val.isEmpty ? 'Enter name' : null, )), ), + if (errorMessages["name"] != null) ...[ + SizedBox(height: 5), // Space before error message + Text( + errorMessages["name"]!, + style: TextStyle(color: Colors.red, fontSize: 12), + ), + ], ], ), SizedBox( @@ -293,10 +298,9 @@ class DepartmentDataState extends State { Text( "Description", style: GoogleFonts.poppins( - fontSize: 12, - fontWeight: FontWeight.w600, - color: Color(0xFF575A74), - ), + fontSize: 12, + fontWeight: FontWeight.w600, + color: Color(0xFF575A74)), ), SizedBox(height: 5), CustomTextFieldForexWrapper( @@ -304,17 +308,15 @@ class DepartmentDataState extends State { isDesktop: widget.isDesktop, color: Colors.transparent, child: SizedBox( - // Increase height for textarea height: 100, child: TextField( controller: controllers["description"], focusNode: focusNodes["description"], style: const TextStyle(fontSize: 12), - maxLines: null, // Allow infinite lines (textarea behavior) - expands: true, // Expands to fill the parent height - decoration: InputDecoration( + maxLines: null, + expands: true, + decoration: const InputDecoration( labelText: "Description", - errorText: errorMessages["description"], labelStyle: TextStyle(fontSize: 11, color: Colors.grey), floatingLabelBehavior: FloatingLabelBehavior.never, border: InputBorder.none, @@ -323,6 +325,13 @@ class DepartmentDataState extends State { ), ), ), + if (errorMessages["description"] != null) ...[ + SizedBox(height: 5), // Space before error message + Text( + errorMessages["description"]!, + style: TextStyle(color: Colors.red, fontSize: 12), + ), + ], ], ), SizedBox(