Postingan lainnya
Redirect dengan mengirimkan pesan error from validation
Asslm WR WB, Salam Koding
Saya sedang membuat CRUD negara, di dalam view_negara.php menampilkan data, update, menambah dan delet data negara saat input form menambah negara dilakukan terdapat form validasi tidak boleh kosong dan data tidak boleh sama.
Permasalahan/Kendalanya: Ketika (form_validation->run() == false) saya melakukan redirect('negara/negara'); tetapi pesan error pada form validasi tidak di munculkan oleh view_negara.php
Solusi yang sudah saya lakukan: Ketika (form_validation->run() == false) saya copy paste isi dari method negara. Sehingga terjadi perulangan yang menurut saya menjadi tidak efektif (tetapi pesan error validasi muncul).
Mohon bantuan/berbagi dari pengalaman para master2 dari kendala tersebut.
controller negara.php
class Negara extends CI_Controller
{
public function negara()
{
//pagination
$config['base_url'] = site_url('negara/negara');
$config['total_rows'] = $this->db->count_all('negara');
$config['per_page'] = 10;
$config['num_links'] = 2;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Prev';
$config['full_tag_open'] = '<div class="pagging text-center"><nav><ul class="pagination justify-content-center">';
$config['full_tag_close'] = '</ul></nav></div>';
$config['num_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['num_tag_close'] = '</span></li>';
$config['cur_tag_open'] = '<li class="page-item active"><span class="page-link">';
$config['cur_tag_close'] = '<span class="sr-only">(current)</span></span></li>';
$config['next_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['next_tagl_close'] = '<span aria-hidden="true">»</span></span></li>';
$config['prev_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['prev_tagl_close'] = '</span>Next</li>';
$config['first_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['first_tagl_close'] = '</span></li>';
$config['last_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['last_tagl_close'] = '</span></li>';
$this->pagination->initialize($config);
$data['page'] = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['pagination'] = $this->pagination->create_links();
$data['negara']=$this->db->get('negara', $config['per_page'],$data['page'])->result();
$this->load->view('view_negara', $data);
}
public function addNegara()
{
$this->form_validation->set_rules('kode', 'Kode Negara', 'required|min_length[3]|numeric|is_unique[negara.kode]');
$this->form_validation->set_rules('name', 'Nama Negara', 'required|min_length[3]|numeric');
if ($this->form_validation->run() == false) {
redirect('negara/negara'); // kendala
//pagination
/* $config['base_url'] = site_url('wilayah/negara');
$config['total_rows'] = $this->db->count_all('negara');
$config['per_page'] = 10;
$config['num_links'] = 2;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Prev';
$config['full_tag_open'] = '<div class="pagging text-center"><nav><ul class="pagination justify-content-center">';
$config['full_tag_close'] = '</ul></nav></div>';
$config['num_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['num_tag_close'] = '</span></li>';
$config['cur_tag_open'] = '<li class="page-item active"><span class="page-link">';
$config['cur_tag_close'] = '<span class="sr-only">(current)</span></span></li>';
$config['next_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['next_tagl_close'] = '<span aria-hidden="true">»</span></span></li>';
$config['prev_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['prev_tagl_close'] = '</span>Next</li>';
$config['first_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['first_tagl_close'] = '</span></li>';
$config['last_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['last_tagl_close'] = '</span></li>';
$this->pagination->initialize($config);
$data['page'] = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['pagination'] = $this->pagination->create_links();
$data['negara']=$this->db->get('negara', $config['per_page'],$data['page'])->result();
$this->load->view('view_negara', $data);
*/
} else{
$this->session->set_flashdata('pesan', 'berhasil');
redirect('negara/negara');
}
}
1 Jawaban:
Alhamdulillah solusi terpecahkan, mungkin ada solusi tambahan dari para master
<pre> if ($this->form_validation->run() == false) { $this->session->set_flashdata('kode', form_error('kode')); $this->session->set_flashdata('name', form_error('name')); $this->session->set_flashdata('value_kode', $this->input->post('kode')); $this->session->set_flashdata('value_name', $this->input->post('name')); redirect('wilayah/negara', 'location');} </pre>