validation in departments
This commit is contained in:
parent
e4acf06234
commit
7e9ea0134b
@ -34,8 +34,19 @@ class DepartmentData extends StatefulWidget {
|
|||||||
class DepartmentDataState extends State<DepartmentData> {
|
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 = {};
|
|
||||||
Map<String, String> errorMessages = {};
|
final Map<String, TextEditingController> controllers = {
|
||||||
|
"name": TextEditingController(),
|
||||||
|
"description": TextEditingController(),
|
||||||
|
};
|
||||||
|
|
||||||
|
final Map<String, FocusNode> focusNodes = {
|
||||||
|
"name": FocusNode(),
|
||||||
|
"description": FocusNode(),
|
||||||
|
};
|
||||||
|
|
||||||
|
final Map<String, String> errorMessages = {};
|
||||||
|
|
||||||
String? selectedName;
|
String? selectedName;
|
||||||
String? selectedDescription;
|
String? selectedDescription;
|
||||||
String? userId;
|
String? userId;
|
||||||
@ -103,7 +114,6 @@ class DepartmentDataState extends State<DepartmentData> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool validateData() {
|
bool validateData() {
|
||||||
|
|
||||||
errorMessages.clear();
|
errorMessages.clear();
|
||||||
|
|
||||||
final data = {
|
final data = {
|
||||||
@ -111,15 +121,17 @@ class DepartmentDataState extends State<DepartmentData> {
|
|||||||
"description": controllers["description"]?.text,
|
"description": controllers["description"]?.text,
|
||||||
};
|
};
|
||||||
|
|
||||||
final requiredFields = [
|
final requiredFields = ["name", "description"];
|
||||||
"name",
|
bool hasFocused = false;
|
||||||
"description"
|
|
||||||
];
|
|
||||||
|
|
||||||
// Check validation for each field
|
|
||||||
for (String field in requiredFields) {
|
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";
|
errorMessages[field] = "Required";
|
||||||
|
|
||||||
|
if (!hasFocused) {
|
||||||
|
focusNodes[field]?.requestFocus();
|
||||||
|
hasFocused = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,12 +142,14 @@ class DepartmentDataState extends State<DepartmentData> {
|
|||||||
print("inside submit");
|
print("inside submit");
|
||||||
userId = await getUserId();
|
userId = await getUserId();
|
||||||
|
|
||||||
setState(() {
|
final isValid = validateData();
|
||||||
// This triggers UI rebuild with error messages
|
setState(() {}); // <-- triggers UI update to show errors
|
||||||
if (validateData()) {
|
|
||||||
postDepartmentData();
|
if (isValid) {
|
||||||
}
|
// 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");
|
||||||
@ -147,7 +161,7 @@ class DepartmentDataState extends State<DepartmentData> {
|
|||||||
|
|
||||||
final departmentData = departmentDetails();
|
final departmentData = departmentDetails();
|
||||||
|
|
||||||
print("initally value of the Department - $departmentData");
|
print("initially value of the Department - $departmentData");
|
||||||
|
|
||||||
final String apiUrldata;
|
final String apiUrldata;
|
||||||
|
|
||||||
@ -160,7 +174,7 @@ class DepartmentDataState extends State<DepartmentData> {
|
|||||||
} else {
|
} else {
|
||||||
print("for add Department id - null");
|
print("for add Department id - null");
|
||||||
apiUrldata = '$apiUrl/api/createDepartment';
|
apiUrldata = '$apiUrl/api/createDepartment';
|
||||||
print("called apiurl - $apiUrldata");
|
print("called apiUrl - $apiUrldata");
|
||||||
departmentData["created_by"] = userId;
|
departmentData["created_by"] = userId;
|
||||||
}
|
}
|
||||||
print("recently Department data - $departmentData");
|
print("recently Department data - $departmentData");
|
||||||
@ -223,7 +237,7 @@ class DepartmentDataState extends State<DepartmentData> {
|
|||||||
Row(
|
Row(
|
||||||
children: [
|
children: [
|
||||||
Text(
|
Text(
|
||||||
'Create Department',
|
(departmentDataId != null) ? 'Edit Department' : 'Create Department',
|
||||||
style: GoogleFonts.poppins(fontSize: 15, color: Colors.black),
|
style: GoogleFonts.poppins(fontSize: 15, color: Colors.black),
|
||||||
),
|
),
|
||||||
const Spacer(),
|
const Spacer(),
|
||||||
@ -252,12 +266,14 @@ class DepartmentDataState extends State<DepartmentData> {
|
|||||||
isDesktop: widget.isDesktop,
|
isDesktop: widget.isDesktop,
|
||||||
color: Colors.transparent,
|
color: Colors.transparent,
|
||||||
child: SizedBox(
|
child: SizedBox(
|
||||||
height: 40,
|
height: 45,
|
||||||
child: TextField(
|
child: TextField(
|
||||||
controller: controllers["name"],
|
controller: controllers["name"],
|
||||||
|
focusNode: focusNodes["name"],
|
||||||
style: const TextStyle(fontSize: 12),
|
style: const TextStyle(fontSize: 12),
|
||||||
decoration: const InputDecoration(
|
decoration: InputDecoration(
|
||||||
labelText: "Department Name",
|
labelText: "Department 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,
|
||||||
@ -292,15 +308,17 @@ class DepartmentDataState extends State<DepartmentData> {
|
|||||||
height: 100,
|
height: 100,
|
||||||
child: TextField(
|
child: TextField(
|
||||||
controller: controllers["description"],
|
controller: controllers["description"],
|
||||||
style: const TextStyle(fontSize: 11),
|
focusNode: focusNodes["description"],
|
||||||
|
style: const TextStyle(fontSize: 12),
|
||||||
maxLines: null, // Allow infinite lines (textarea behavior)
|
maxLines: null, // Allow infinite lines (textarea behavior)
|
||||||
expands: true, // Expands to fill the parent height
|
expands: true, // Expands to fill the parent height
|
||||||
decoration: const InputDecoration(
|
decoration: 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,
|
||||||
contentPadding: EdgeInsets.symmetric(vertical: 16, horizontal: 8),
|
contentPadding: EdgeInsets.symmetric(vertical: 16),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user