CodeIgniter form_validation->run() always returns false?
I think this is happening because you have not set any validation rules.Controller code should look like this:
public function write_prof_review($prof_id){ $this->load->model('Queries'); // form stuff here $this->load->helper('form'); $this->load->library('form_validation'); $data['prof_id'] = $prof_id; // here it is; I am binding rules $this->form_validation->set_rules('course_code', 'Course Code', 'required'); $this->form_validation->set_rules('easiness', 'easiness', 'required'); $this->form_validation->set_rules('helpfulness', 'helpfulness', 'required'); if($this->form_validation->run() == FALSE) { $this->load->view('create_prof_review', $data); } else { $this->Queries->submit_prof_review($prof_id, $this->USERID); $this->load->view('form_submitted'); }}
Please refer to the CodeIgniter user guide; it will give you more information about validation rules.
I had the same problem though the cause was different. I was missing one of the input fields that I was validating from the form i.e
private function validate_data(){ $validate_data = array( array( 'field' => 'steps', 'label' => 'Steps', 'rules' => 'trim|required|integer|xss_clean' ), array( 'field' => 'pace', 'label' => 'Pace', 'rules' => 'trim|required|integer|xss_clean' ), array( 'field' => 'speed', 'label' => 'Speed', 'rules' => 'trim|required|numeric|xss_clean' ), array( 'field' => 'distance', 'label' => 'Distance', 'rules' => 'trim|required|numeric|xss_clean' ) );//end array validate_data return $validate_data; }
I was missing the speed input field in the form. I added it and the problem was solved. It really gave me a headache cause I was just reusing code that I have used so many times, so I could not understand why ($this->form_validation->run()
was returning false, something that I had never experienced before.
You can go to system/library/Form_validation.php
and in
if (count($this->_config_rules) == 0){return FALSE;}
change false to true