diff --git a/app/Controllers/LeadsController.php b/app/Controllers/LeadsController.php index c09b0e43..713c7ecc 100644 --- a/app/Controllers/LeadsController.php +++ b/app/Controllers/LeadsController.php @@ -465,7 +465,7 @@ class LeadsController extends BaseController // print_r($rules); die; - if (isset($data['lead_form_type']) && (int)$data['lead_form_type'] === 2) { + if ((int)$this->request->getPost('lead_form_type') === 2) { $rules['client_type'] = [ 'rules' => 'required', 'errors' => ['required' => 'Client Type is required'] @@ -482,6 +482,51 @@ class LeadsController extends BaseController 'rules' => 'required', 'errors' => ['required' => 'Date of Expiry is required'] ]; + + if ((int)$this->request->getPost('claim_history') === 1) { + $rules['first_year.*'] = [ + 'rules' => 'required|regex_match[/^\d{4}-\d{4}$/]', + 'errors' => ['required' => 'Claim Year is required for all entries.','regex_match' => 'Year must be in format YYYY-YYYY.'] + ]; + $rules['first_policy_type_.*'] = [ + 'rules' => 'required|regex_match[/^[a-zA-Z0-9_-]+$/]', + 'errors' => ['required' => 'Policy Type is required in Claim History.', + 'regex_match' => 'Policy Type only letters, numbers, space, hyphens and underscores are allowed' + ] + ]; + $rules['first_date_of_loss_.*'] = [ + 'rules' => 'required|regex_match[/^[0-9]{2}-[0-9]{2}-[0-9]{4}$/]', + 'errors' => ['required' => 'Date of Loss is required.', + 'regex_match' => 'Date of Loss must be inValid format.'] + ]; + $rules['first_cause_of_loss.*'] = [ + 'rules' => 'required|regex_match[/^[a-zA-Z0-9\s_-]+$/]', + 'errors' => [ + 'required' => 'Cause of Loss is required.', + 'regex_match' => 'Cause of Loss only letters, numbers, space, hyphens and underscores are allowed' + ] + ]; + $rules['first_claim_amount.*'] = [ + 'rules' => 'required|numeric', + 'errors' => [ + 'required' => 'Claim Amount is required.', + 'numeric' => 'Claim Amount must be a number.' + ] + ]; + $rules['first_settled_amount.*'] = [ + 'rules' => 'required|numeric', + 'errors' => ['required' => 'Settled Amount is required.', + 'numeric' => 'Settled Amount must be a number.'] + ]; + $rules['first_claim_status.*'] = [ + 'rules' => 'required|alpha_space', + 'errors' => [ + 'required' => 'Claim Status is required.', + 'alpha_space' => 'Claim Status should only contain letters and spaces.' + ] + ]; + } + } // 1. MANUALLY VALIDATE FILES BEFORE PROCESSING @@ -516,7 +561,41 @@ class LeadsController extends BaseController // } // } - if (!$this->validate($rules)) { + + $isValid = $this->validate($rules); + + $start_dates = $this->request->getPost('policy_start_date'); + $end_dates = $this->request->getPost('policy_end_date'); + + if ($start_dates && $end_dates) { + + $starts = is_array($start_dates) ? $start_dates : [$start_dates]; + $ends = is_array($end_dates) ? $end_dates : [$end_dates]; + + foreach ($starts as $index => $s_date) { + $e_date = $ends[$index] ?? null; + + if ($s_date && $e_date) { + + $f_start = change_date_format($s_date, 'd/m/Y', 'Y-m-d'); + $f_end = change_date_format($e_date, 'd/m/Y', 'Y-m-d'); + + if ($f_start && $f_end && ($f_end < $f_start)) { + $isValid = false; + + // Determine the error key name + // If it's an array, we use index (e.g., policy_end_date.0) + $errorKey = is_array($start_dates) ? "policy_end_date.$index" : "policy_end_date"; + + $this->validator->setError($errorKey, 'Date of Expiry cannot be before Date of Commencement.'); + } + } + } + } + + + if (!$isValid) { + return $this->response->setStatusCode(400)->setJSON([ 'status' => false, 'message' => 'Input validation failed', diff --git a/app/Controllers/PolicyTransactionController.php b/app/Controllers/PolicyTransactionController.php index 838c646e..e390e00d 100644 --- a/app/Controllers/PolicyTransactionController.php +++ b/app/Controllers/PolicyTransactionController.php @@ -1052,7 +1052,47 @@ class PolicyTransactionController extends BaseController 'exp_amt.*' => ['label' => 'Expected Amount', 'rules' => 'permit_empty|decimal', 'errors' => ['decimal' => 'The Expected Amount field must contain a valid number.']] ]; - if (!$this->validate($rules)) { + + $isValid = $this->validate($rules); + + // $Date_of_commencement = $this->request->getPost('policy_start_date'); + // $Date_of_expiry = $this->request->getPost('policy_end_date'); + + // if ($Date_of_commencement && $Date_of_expiry && ($Date_of_expiry < $Date_of_commencement)) { + // $this->validator->setError('policy_end_date', '$Date of Commencement and Date of Expiry are mismatched (Date of Expiry cannot be before $Date of Commencement).'); + // $isValid = false; + // } + + $start_dates = $this->request->getPost('policy_start_date'); + $end_dates = $this->request->getPost('policy_end_date'); + + if ($start_dates && $end_dates) { + // Standardize: If it's a single string (Type 2), wrap it in an array + $starts = is_array($start_dates) ? $start_dates : [$start_dates]; + $ends = is_array($end_dates) ? $end_dates : [$end_dates]; + + foreach ($starts as $index => $s_date) { + $e_date = $ends[$index] ?? null; + + if ($s_date && $e_date) { + + $f_start = change_date_format($s_date, 'Y-m-d', 'Y-m-d'); // be carefully here we made so many times mistakes on here + $f_end = change_date_format($e_date, 'Y-m-d', 'Y-m-d');// be carefully here we made so many times mistakes on here + + if ($f_start && $f_end && ($f_end < $f_start)) { + $isValid = false; + + // Determine the error key name + // If it's an array, we use index (e.g., policy_end_date.0) + $errorKey = is_array($start_dates) ? "policy_end_date.$index" : "policy_end_date"; + + $this->validator->setError($errorKey, 'D.O.E(Date of Expiry) cannot be before D.O.C(Date of Commencement).'); + } + } + } + } + + if (!$isValid) { return $this->response->setStatusCode(400)->setJSON([ 'status' => false, 'message' => 'Input validation failed', diff --git a/app/Views/leads_non_eb.php b/app/Views/leads_non_eb.php index 8f5a82f8..6456935a 100644 --- a/app/Views/leads_non_eb.php +++ b/app/Views/leads_non_eb.php @@ -1027,7 +1027,7 @@
- $year"; @@ -1036,27 +1036,27 @@
- +
- +
- +
- +
- +
- +
@@ -1072,6 +1072,16 @@ referenceDiv.insertAdjacentHTML('beforeend', claimsFields); + let isChecked = $("#claim_history").length > 0 ? $("#claim_history").prop("checked") : true; + let lastRow = $(".claim-row").last(); + if (isChecked) { + lastRow.show(); + lastRow.find(".claim-input").attr("required", true); + } else { + lastRow.hide(); + lastRow.find(".claim-input").removeAttr("required"); + } + // Increment claim index increment = increment + 1; @@ -1143,9 +1153,15 @@ // No rows yet, so create them appendThreeYearsClaims(); } + $(".claim-input").attr("required", true); } else { $(".claim-row").hide(); + $(".claim-input").removeAttr("required"); // Required Attributes Remove + $(".claim-input").val(""); // Value Reset + $(".claim-input").each(function() { + $(this).parsley().reset(); // Validation Reset + }); } }