简体   繁体   中英

Code Igniter Validation on Error

I am pretty knew to code igniter / OOP but I am giving it a shot and a little stumped at this point.

I have 2 functions. One is the search which loads data from a model. The other is the save function.

My issue is, when running save() I am getting errors because its trying ti display the validation errors but we no longer have the data from the database that we got from the search() function.

I feel that it would be redundant to include all the details form the search back into this save function which is why I think I am doing something wrong.

public function search()
    {

        // Define some vars
        $data['title'] = 'Submit Attrition';
        $data['js_file'] = 'submit.js';

        // Load our helper
        $this->load->helper('form');

        // Get the user and pass it to the model
        $empQID = $this->input->post('empQID');  
        $data['userDetails'] = $this->submit_model->get_details($empQID);
        $data['languages'] = $this->submit_model->get_languages();
        $data['types'] = $this->submit_model->get_types();
        $data['ratings'] = $this->submit_model->get_ratings();
        $data['processes'] = $this->submit_model->get_processes();

        // Send the data to the views       
        $this->load->view('templates/header', $data);
        $this->load->view('submit/search', $data);
        $this->load->view('templates/footer', $data);
    }

    /**
    * Validate & save attrition submission
    *
    * @author   Carl
    * @return   void
    */
    public function save()
    {
        $data['title'] = 'Submit Attrition';
        $this->load->library('form_validation');
        $this->form_validation->set_rules('language', 'Supporting Language', 'required');

        // Validation failed, show form w/ validation errors
        if ($this->form_validation->run() === FALSE)
        {
            $this->load->view('templates/header', $data);
            $this->load->view('submit/search', $data);
            $this->load->view('templates/footer', $data);
        }
        else
        {
            // Success : Send data to model
            $this->submit_model->save_attrition();
            $this->load->view('templates/header', $data);
            $this->load->view('submit/success', $data);
            $this->load->view('templates/footer', $data);
        }   
    }

I'm not sure I entirely understand your question, but I think I understand what you're trying to do. In CodeIgniter, you do something like this:

class MyController
{

    // this controller action will load whether the form is submitted or not
    // $user_id is set by the router
    public function save($username)
    {

        // if the users not in the database, throw a 404 error
        $user = $this->db->get_where('users', ['username' => $username]);
        if(!$user) {
            return show_404();
        }

        // if it's not a post request, then the form hasn't been submitted
        // so don't bother validating it
        if($router->fetch_method() === 'POST' && $form_validation->run() === TRUE)
        {
            $user['name'] = $this->input->post('name');
            $this->db->update('users', $user);
            $this->load->view('success_page');

            // return so we don't render the form again
            return;
        }

        // this will happen in all cases, __except__ when the form was submitted
        // with valid data 
        $this->load->view('form');

    }
}

I've skipped on details for brevity (like loading the relevant libraries), and because I can't remember all the CI syntax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM