Update mencegah input contact agar tidak ada duplikasi inisial
This commit is contained in:
parent
f67960311b
commit
16769a3998
@ -180,7 +180,8 @@ $routes->get('/testemail','Activites::email_test');
|
||||
$routes->get('/contacts', 'Contacts::index');
|
||||
$routes->get('/contacts/view/(:num)', 'Contacts::view/$1');
|
||||
$routes->match(['get','post'],'/contacts/edit/(:num)', 'Contacts::edit/$1');
|
||||
$routes->match(['get','post'],'/contacts/create', 'Contacts::edit/0');
|
||||
// $routes->match(['get','post'],'/contacts/create', 'Contacts::edit/0');
|
||||
$routes->post('/contacts/create', 'Contacts::createFromActivities');
|
||||
|
||||
// emails
|
||||
$routes->get('/emails', 'Emails::index');
|
||||
|
||||
@ -31,18 +31,38 @@ class Contacts extends Controller {
|
||||
public function edit($contactid = null) {
|
||||
$db = \Config\Database::connect();
|
||||
$data = array();
|
||||
|
||||
if($contactid != 0) {
|
||||
$sql = "select * from contacts where contactid='$contactid'";
|
||||
$query = $db->query($sql);
|
||||
$results = $query->getResultArray();
|
||||
$data['contacts'] = $results;
|
||||
}
|
||||
|
||||
if ($this->request->getMethod() === 'POST') {
|
||||
|
||||
if ($contactid != 0) {
|
||||
$ruleInitial = "required|is_unique[contacts.initial,contactid,{$contactid}]";
|
||||
} else {
|
||||
$ruleInitial = "required|is_unique[contacts.initial]";
|
||||
}
|
||||
$rules = [
|
||||
// 'contactid' => 'required',
|
||||
'firstname' => 'required',
|
||||
'email_1' => 'required',
|
||||
'initial' => 'required'
|
||||
'email_1' => 'required|valid_email',
|
||||
'initial' => $ruleInitial
|
||||
];
|
||||
$messages = [
|
||||
'firstname' => [
|
||||
'required' => 'Nama depan harus diisi, tidak boleh kosong.'
|
||||
],
|
||||
'email_1' => [
|
||||
'required' => 'Email wajib diisi.',
|
||||
'valid_email' => 'Format email yang anda masukkan tidak valid.'
|
||||
],
|
||||
'initial' => [
|
||||
'required' => 'Initial tidak boleh kosong.',
|
||||
'is_unique' => 'Initial ini sudah dipakai orang lain, silakan cari yang unik.'
|
||||
]
|
||||
];
|
||||
$data['new_value'] = [
|
||||
'firstname' => $this->request->getVar('firstname'),
|
||||
@ -58,7 +78,7 @@ class Contacts extends Controller {
|
||||
'siteid' => $this->request->getVar('siteid') ?? null //Untuk Create dari AR
|
||||
];
|
||||
|
||||
if($this->validate($rules)){
|
||||
if($this->validate($rules, $messages)){
|
||||
if($contactid != 0) {
|
||||
$contactsModel = new contactsModel();
|
||||
$contactsModel->set('enddate', NULL);
|
||||
@ -90,4 +110,71 @@ class Contacts extends Controller {
|
||||
}
|
||||
return view('contacts_editor', $data);
|
||||
}
|
||||
|
||||
public function createFromActivities() {
|
||||
$db = \Config\Database::connect();
|
||||
|
||||
$rules = [
|
||||
'firstname' => 'required',
|
||||
'email_1' => 'required|valid_email',
|
||||
'initial' => 'required|is_unique[contacts.initial]'
|
||||
];
|
||||
$messages = [
|
||||
'firstname' => [
|
||||
'required' => 'Nama depan harus diisi, tidak boleh kosong.'
|
||||
],
|
||||
'email_1' => [
|
||||
'required' => 'Email wajib diisi.',
|
||||
'valid_email' => 'Format email yang anda masukkan tidak valid.'
|
||||
],
|
||||
'initial' => [
|
||||
'required' => 'Initial tidak boleh kosong.',
|
||||
'is_unique' => 'Initial ini sudah dipakai orang lain, silakan cari yang unik.'
|
||||
]
|
||||
];
|
||||
$data['new_value'] = [
|
||||
'firstname' => $this->request->getVar('firstname'),
|
||||
'lastname' => $this->request->getVar('lastname'),
|
||||
'title' => $this->request->getVar('title'),
|
||||
'initial' => $this->request->getVar('initial'),
|
||||
'birthdate' => ($this->request->getVar('birthdate') == '') ? NULL : $this->request->getVar('birthdate'),
|
||||
'email_1' => $this->request->getVar('email_1'),
|
||||
'email_2' => $this->request->getVar('email_2'),
|
||||
'phone' => $this->request->getVar('phone'),
|
||||
'mobile_1' => $this->request->getVar('mobile_1'),
|
||||
'mobile_2' => $this->request->getVar('mobile_2'),
|
||||
'siteid' => $this->request->getVar('siteid') ?? null //Untuk Create dari AR
|
||||
];
|
||||
|
||||
if ($this->validate($rules, $messages)) {
|
||||
|
||||
$contactsModel = new ContactsModel();
|
||||
$contactsModel->set('createdate', 'NOW()', FALSE);
|
||||
$contactsModel->set('enddate', NULL);
|
||||
$contactsModel->insert($data['new_value']);
|
||||
$contactid = $contactsModel->getInsertID();
|
||||
|
||||
if ($this->request->getVar('siteid') != null) {
|
||||
$siteContactModel = new SiteContactModel();
|
||||
$siteContactValue = [
|
||||
'siteid' => $this->request->getVar('siteid'),
|
||||
'contactid' => $contactid,
|
||||
'contactemail' => $this->request->getVar('email_1'),
|
||||
];
|
||||
$siteContactModel->insert($siteContactValue);
|
||||
}
|
||||
|
||||
// Kembalikan JSON Sukses
|
||||
return $this->response->setJSON(['status' => 'success', 'message' => 'Data berhasil disimpan']);
|
||||
|
||||
} else {
|
||||
// --- JIKA VALIDASI GAGAL ---
|
||||
// Ubah HTTP Status menjadi 400 (Bad Request) agar AJAX tahu ini adalah "Error"
|
||||
return $this->response->setStatusCode(400)->setJSON([
|
||||
'status' => 'error',
|
||||
'errors' => $this->validator->getErrors() // Ambil list errornya
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -26,9 +26,9 @@
|
||||
</div>
|
||||
|
||||
<div class="row mb-3 align-items-center">
|
||||
<label for="lastname" class="col-sm-3 col-form-label">Last Name <span class="text-danger">*</span></label>
|
||||
<label for="lastname" class="col-sm-3 col-form-label">Last Name </label>
|
||||
<div class="col-sm-9">
|
||||
<input type="text" class="form-control form-control-sm" id="lastname" name="lastname" required>
|
||||
<input type="text" class="form-control form-control-sm" id="lastname" name="lastname">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -265,6 +265,57 @@ function deleteTrainingRow(btn, contactid) {
|
||||
flatpickr(".trainingdate", { allowInput: true, dateFormat: "Y-m-d" });
|
||||
|
||||
|
||||
// $('#formCreateContact').submit(function(e) {
|
||||
// // 1. Cegah form melakukan reload halaman bawaan HTML
|
||||
// e.preventDefault();
|
||||
|
||||
// // 2. Ambil URL action dan data dari form
|
||||
// var url = $(this).attr('action');
|
||||
// var formData = $(this).serialize();
|
||||
|
||||
// // 3. Kirim data ke controller menggunakan AJAX POST
|
||||
// $.ajax({
|
||||
// type: "POST",
|
||||
// url: url,
|
||||
// data: formData,
|
||||
// success: function(response) {
|
||||
// // Tutup modal dan bersihkan isian formnya
|
||||
// $('#createContactModal').modal('hide');
|
||||
// $('#formCreateContact')[0].reset();
|
||||
|
||||
// // Tampilkan pesan sukses
|
||||
// alert('Contact berhasil ditambahkan!');
|
||||
|
||||
// // --- INI BAGIAN REFRESH-NYA ---
|
||||
// var siteid = $('#siteid').val();
|
||||
// var current_actid = $('#current_actid').val();
|
||||
|
||||
// // Siapkan format url untuk current_actid (sama seperti logika sebelumnya)
|
||||
// if (current_actid !== '' && current_actid !== undefined) {
|
||||
// current_actid = '/' + current_actid;
|
||||
// } else {
|
||||
// current_actid = '';
|
||||
// }
|
||||
|
||||
// // Jalankan ulang $.get milikmu khusus untuk training_form
|
||||
// if(siteid !== '') {
|
||||
// $.get("<?=base_url();?>/activities/getsitecontact/" + siteid + current_actid, function(data) {
|
||||
// $('#training_form').html(data);
|
||||
|
||||
// // Re-inisialisasi select2 agar tampilannya kembali rapi
|
||||
// $('.select2').select2({
|
||||
// theme: 'bootstrap-5',
|
||||
// width: '100%'
|
||||
// });
|
||||
// });
|
||||
// }
|
||||
// },
|
||||
// error: function(xhr, status, error) {
|
||||
// alert('Terjadi kesalahan saat menyimpan data!');
|
||||
// console.error(xhr.responseText);
|
||||
// }
|
||||
// });
|
||||
// });
|
||||
$('#formCreateContact').submit(function(e) {
|
||||
// 1. Cegah form melakukan reload halaman bawaan HTML
|
||||
e.preventDefault();
|
||||
@ -278,6 +329,7 @@ $('#formCreateContact').submit(function(e) {
|
||||
type: "POST",
|
||||
url: url,
|
||||
data: formData,
|
||||
dataType: "json", // [UBAHAN 1] Beritahu AJAX bahwa kita mengharapkan balasan JSON
|
||||
success: function(response) {
|
||||
// Tutup modal dan bersihkan isian formnya
|
||||
$('#createContactModal').modal('hide');
|
||||
@ -286,7 +338,7 @@ $('#formCreateContact').submit(function(e) {
|
||||
// Tampilkan pesan sukses
|
||||
alert('Contact berhasil ditambahkan!');
|
||||
|
||||
// --- INI BAGIAN REFRESH-NYA ---
|
||||
// --- INI BAGIAN REFRESH-NYA (TIDAK ADA YANG DIUBAH) ---
|
||||
var siteid = $('#siteid').val();
|
||||
var current_actid = $('#current_actid').val();
|
||||
|
||||
@ -311,8 +363,21 @@ $('#formCreateContact').submit(function(e) {
|
||||
}
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
alert('Terjadi kesalahan saat menyimpan data!');
|
||||
console.error(xhr.responseText);
|
||||
// [UBAHAN 2] Tangkap error 400 dari validasi Controller
|
||||
if (xhr.status === 400 && xhr.responseJSON && xhr.responseJSON.errors) {
|
||||
var errorMessages = "Gagal menyimpan data karena:\n\n";
|
||||
|
||||
// Looping semua error (misal firstname kosong, atau initial duplikat)
|
||||
$.each(xhr.responseJSON.errors, function(key, value) {
|
||||
errorMessages += "• " + value + "\n";
|
||||
});
|
||||
|
||||
alert(errorMessages);
|
||||
} else {
|
||||
// Error sistem lainnya (misal server down atau koneksi putus)
|
||||
alert('Terjadi kesalahan saat menyimpan data!');
|
||||
console.error(xhr.responseText);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user