validation in departments

This commit is contained in:
VE10-Sanjeev 2025-05-22 13:01:12 +00:00
parent 3474a1dbca
commit 157b329de1

View File

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