fix(z-task-modal): fixed validation logic for employees, at least 1 employee is required

This commit is contained in:
Matej Glasnák committed 2025-05-09 13:22:30 +02:00
1 parent f7b81b632f
commit 3f75f9d204
1 file changed
+32 -17
+32 -17
View File
@@ -635,6 +635,8 @@ export default {
if (!(await this.v$.$validate())) return; if (!(await this.v$.$validate())) return;
if (!employeesValid || !timeblocksValid) return; if (!employeesValid || !timeblocksValid) return;
return true;
}, },
async handleOnRemoveEmployee() { async handleOnRemoveEmployee() {
@@ -757,28 +759,41 @@ export default {
this.employeeValidations = []; this.employeeValidations = [];
if (this.subtask.id) { if (this.subtask.id) {
if (this.subtask.employee && this.subtask.duration) return; // For existing subtasks, require both employee and duration
if (!this.subtask.employee_id || !this.subtask.duration) {
this.employeeValidations[0] = {}; this.employeeValidations[0] = {
employee: !this.subtask.employee_id,
if (!this.subtask.employee_id) duration: !this.subtask.duration,
this.employeeValidations[0].employee = true; };
}
if (!this.subtask.duration)
this.employeeValidations[0].duration = true;
} else { } else {
// For new subtasks, only validate non-empty employee entries // For new subtasks, require at least one employee with duration
let hasValidEmployee = false;
this.subtask.employees.forEach((employee, index) => { this.subtask.employees.forEach((employee, index) => {
// Skip validation if both id and duration are empty // Skip validation if both id and duration are empty
if (!employee.id && !employee.duration) return; if (!employee.id && !employee.duration) {
// If this is the first employee and no valid employee exists yet, show validation
if (index === 0 && !hasValidEmployee) {
this.employeeValidations[index] = {
employee: true,
duration: true,
};
}
return;
}
// Only validate if either id or duration is filled // Validate if either id or duration is filled
if (employee.id || employee.duration) { if (employee.id || employee.duration) {
this.employeeValidations[index] = {}; this.employeeValidations[index] = {
employee: !employee.id,
duration: !employee.duration,
};
if (!employee.id) this.employeeValidations[index].employee = true; // If both id and duration are present, mark as valid
if (!employee.duration) if (employee.id && employee.duration) {
this.employeeValidations[index].duration = true; hasValidEmployee = true;
}
} }
}); });
} }
@@ -855,7 +870,7 @@ export default {
subtask: { subtask: {
handler(val) { handler(val) {
if (this.editMode && !this.subtask?.id) { if (this.editMode && !this.subtask?.id) {
const employees = val.employees.filter( const employees = val?.employees?.filter(
(employee) => employee?.id || employee?.duration, (employee) => employee?.id || employee?.duration,
) || [{}]; ) || [{}];