From 3f75f9d2046ac5271c8d57b78771865ba8915e38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Glasn=C3=A1k?= Date: Fri, 9 May 2025 13:22:30 +0200 Subject: [PATCH] fix(z-task-modal): fixed validation logic for employees, at least 1 employee is required --- .../plugins/tm@modal/task/z-task-modal.vue | 49 ++++++++++++------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/app/src/plugins/tm@modal/task/z-task-modal.vue b/app/src/plugins/tm@modal/task/z-task-modal.vue index b51c164..7fdc2e6 100644 --- a/app/src/plugins/tm@modal/task/z-task-modal.vue +++ b/app/src/plugins/tm@modal/task/z-task-modal.vue @@ -635,6 +635,8 @@ export default { if (!(await this.v$.$validate())) return; if (!employeesValid || !timeblocksValid) return; + + return true; }, async handleOnRemoveEmployee() { @@ -757,28 +759,41 @@ export default { this.employeeValidations = []; if (this.subtask.id) { - if (this.subtask.employee && this.subtask.duration) return; - - this.employeeValidations[0] = {}; - - if (!this.subtask.employee_id) - this.employeeValidations[0].employee = true; - - if (!this.subtask.duration) - this.employeeValidations[0].duration = true; + // For existing subtasks, require both employee and duration + if (!this.subtask.employee_id || !this.subtask.duration) { + this.employeeValidations[0] = { + employee: !this.subtask.employee_id, + duration: !this.subtask.duration, + }; + } } 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) => { // 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) { - this.employeeValidations[index] = {}; + this.employeeValidations[index] = { + employee: !employee.id, + duration: !employee.duration, + }; - if (!employee.id) this.employeeValidations[index].employee = true; - if (!employee.duration) - this.employeeValidations[index].duration = true; + // If both id and duration are present, mark as valid + if (employee.id && employee.duration) { + hasValidEmployee = true; + } } }); } @@ -855,7 +870,7 @@ export default { subtask: { handler(val) { if (this.editMode && !this.subtask?.id) { - const employees = val.employees.filter( + const employees = val?.employees?.filter( (employee) => employee?.id || employee?.duration, ) || [{}];