php - codeigniter CMS form validation -
firstly im new codeigniter , mvc.
i creating cms , coudln't decide route take have 2 applications (front end/cms) or create admin controller. opted 1 application , creating admin via controller.
doing way have ran problem form validation if doesn't validate cant load form have redirect means wont repopulate unvalidated fields. use variable in 3rd uri segment determine whether display form inserting new record, populated form editing record, or tabled list of records.
the form posts /admin/videos/save
function videos() { if (!$this->tank_auth->is_logged_in()) { redirect('/auth/login/'); } else { $this->load->model('videos_model'); $data['section'] = "videos"; $data['area'] = "videos"; $data['mode'] = $this->uri->segment(3, 'create'); $data['user_id'] = $this->tank_auth->get_user_id(); $data['username'] = $this->tank_auth->get_username(); if ($data['mode'] == 'edit') { $data['id'] = $this->uri->segment(4); $data['videos'] = $this->videos_model->get_videos($data['id']); } elseif ($data['mode'] == 'list') { if ($this->uri->segment(4)) { $data['filter'] = $this->uri->segment(4); $data['videos'] = $this->videos_model->get_filtered_videos($data['filter']); } else { $data['videos'] = $this->videos_model->get_filtered_videos(); } } elseif ($data['mode'] == 'save') { $this->load->helper('form'); $this->load->library('form_validation'); $this->form_validation->set_rules('videotitle', 'title', 'required'); $this->form_validation->set_rules('code', 'youtube code', 'required'); if ($this->form_validation->run() === false) { redirect('/admin/videos'); } else { $this->videos_model->set_videos(); redirect('/admin/videos/list'); } } if ($data['mode'] != "create" && empty($data['videos'])) { show_404(); } $this->load->view('admin/templates/head', $data); $this->load->view('admin/templates/body_navbar', $data); $this->load->view('admin/videos', $data); $this->load->view('admin/templates/footer', $data); } }
am setting wrong way, should use 2 application folders or have 3 controllers editing/inserting/viewing all. or there solution current setup?
i haven't used codeigniter's form helper nor validation lib, excuse ignorance, there particular reason you're not doing ajax post instead?
am setting wrong way, should use 2 application folders or have 3 controllers editing/inserting/viewing all. or there solution current setup?
why 3 controllers? can have single controller multiple functions. honestly, i'd recommend doing simple ajax post on form , returning json data whether validation passed or not -- no need redirects.
something like:
// ajax
function validateform() { $.post('route/to/controller', {"apple": applevalue, "peach": peachvalue}, function(data) { json = $.parsejson(data); if (json.success) alert('great!'); else alert('nope!'); });
//controller
function validateform() { $data['success'] = ...validation checks... echo json_encode($data); }
Comments
Post a Comment