diff --git a/app/Controllers/ClientController.php b/app/Controllers/ClientController.php index 15df403f..ee7ae376 100755 --- a/app/Controllers/ClientController.php +++ b/app/Controllers/ClientController.php @@ -134,11 +134,11 @@ class ClientController extends AdminController public function validateDuplicateByClientBranch() { - $request = service('request'); - $value = $request->getPost('value'); - $clientId = $request->getPost('client_id'); - $branchId = $request->getPost('branch_id'); - $isDuplicate = $this->clientModel->isDuplicateByClientBranch($value, $clientId, $branchId); + $value = $this->request->getPost('value'); + $clientId = $this->request->getPost('client_id'); + $branchId = $this->request->getPost('branch_id'); + $field = $this->request->getPost('field'); + $isDuplicate = $this->clientModel->isDuplicateByClientBranch($value, $field, $clientId, $branchId); return $this->response->setJSON(['isDuplicate' => $isDuplicate]); } public function checkDuplicateTableFieldValue() diff --git a/app/Models/ClientModel.php b/app/Models/ClientModel.php index 09db986d..b84bfae1 100755 --- a/app/Models/ClientModel.php +++ b/app/Models/ClientModel.php @@ -226,12 +226,12 @@ class ClientModel extends Model return $result; } - public function isDuplicateByClientBranch($email, $clientId, $branchId) + public function isDuplicateByClientBranch($value, $field, $clientId, $branchId) { $builder = $this->db->table('level_contacts lc') ->select('lc.id') ->join('client_branch cb', 'lc.ref_id = cb.id', 'left') - ->where('lc.email', $email) + ->where('lc.'.$field, $value) ->where('lc.contact_type', 'client') ->where('cb.client_id', $clientId) ->where('lc.ref_id', $branchId) diff --git a/app/Views/client_branch.php b/app/Views/client_branch.php index 024c3741..6673a4af 100755 --- a/app/Views/client_branch.php +++ b/app/Views/client_branch.php @@ -198,12 +198,13 @@
+ name="email[]" id="email" data-parsley-trigger="change" data-parsley-type="email" onchange="validateDuplicateByClientBranch(this, 'email','branchBtnSubmit')" required>
- +
- +
@@ -972,17 +973,23 @@ function validateInput(input, table, field, submitButId){ } -function validateDuplicateByClientBranch(input, submitButId) { + + +function validateDuplicateByClientBranch(input, field, submitButId) { let value = $(input).val().trim(); let clientId = $('#client_id_branch').val(); let branchId = $('#branch_id_primarykey').val(); - console.log(`Ln968 cId: ${clientId} | bId: ${branchId}`); + let label = $(input).closest('.form-group').find('label').text().replace('*', '').trim(); + let message = label ? label + " is duplicate!" : "Value is duplicate!"; + + console.log(`cId: ${clientId} | bId: ${branchId}`); // Don't forgot be careful // 1 Local duplication check (User entered) let isLocalDuplicate = false; - $('input[name="email[]"]').each(function() { - if (this !== input && $(this).val().trim() === value && value !== '') { + $('input[name="' + field + '[]"]').each(function(index) { + console.log(`Entered value: ${value} | Contact ${index+1} value: ${$(this).val()}`); + if (this !== input && $(this).val().trim() === value) { isLocalDuplicate = true; return false; // break loop } @@ -990,7 +997,8 @@ function validateDuplicateByClientBranch(input, submitButId) { if (isLocalDuplicate) { console.log(`r u n Local`); - toastr.warning("Email is duplicate!", 'WARNING'); + console.log(`btn Dis - true`); + toastr.warning(message, 'WARNING'); $('#' + submitButId).prop('disabled', true); return; // don’t call server if duplicate in UI } @@ -998,21 +1006,25 @@ function validateDuplicateByClientBranch(input, submitButId) { // Don't forgot be careful // 2 Server-side duplicate check (DB) if (!isLocalDuplicate && value !== '') { - console.log(`r u n Server`); + $.ajax({ url: '', type: 'POST', data: { client_id: clientId, branch_id: branchId, - value: value + value: value, + field: field }, dataType: 'json', success: function(response) { if (response.isDuplicate) { - toastr.warning("Email already exists!", 'WARNING'); + console.log(`r u n Server`); + console.log(`btn Dis - true`); + toastr.warning(message, 'WARNING'); $('#' + submitButId).prop('disabled', true); } else { + console.log(`btn Dis - false`); $('#' + submitButId).prop('disabled', false); } },