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();
Map<String, dynamic>? apiData;
final Map<String, TextEditingController> controllers = {
"name": TextEditingController(),
"description": TextEditingController(),
};
final Map<String, FocusNode> focusNodes = {
"name": FocusNode(),
"description": FocusNode(),
};
final Map<String, String> errorMessages = {};
final Map<String, TextEditingController> controllers = {};
Map<String, String> errorMessages = {};
String? selectedName;
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() {
print("Inside Update Function - ${widget.departmentData}");
@ -124,6 +128,7 @@ class DepartmentDataState extends State<DepartmentData> {
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<DepartmentData> {
}
Future<void> 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<void> postDepartmentData({int isActive = 1}) async {
@ -260,28 +261,32 @@ class DepartmentDataState extends State<DepartmentData> {
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<DepartmentData> {
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<DepartmentData> {
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<DepartmentData> {
),
),
),
if (errorMessages["description"] != null) ...[
SizedBox(height: 5), // Space before error message
Text(
errorMessages["description"]!,
style: TextStyle(color: Colors.red, fontSize: 12),
),
],
],
),
SizedBox(