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> {
|
||||
final ApiService apiService = ApiService();
|
||||
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? selectedDescription;
|
||||
String? userId;
|
||||
@ -103,7 +114,6 @@ class DepartmentDataState extends State<DepartmentData> {
|
||||
}
|
||||
|
||||
bool validateData() {
|
||||
|
||||
errorMessages.clear();
|
||||
|
||||
final data = {
|
||||
@ -111,15 +121,17 @@ class DepartmentDataState extends State<DepartmentData> {
|
||||
"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<DepartmentData> {
|
||||
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<DepartmentData> {
|
||||
|
||||
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<DepartmentData> {
|
||||
} 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<DepartmentData> {
|
||||
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<DepartmentData> {
|
||||
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<DepartmentData> {
|
||||
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),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
Loading…
Reference in New Issue
Block a user