Codeigniter中的验证错误

时间:2014-05-26 12:10:32

标签: php codeigniter validation

我可以知道为什么我的插入函数会首先显示验证消息,即使我还没有插入任何数据。我进入插入页面,它显示验证消息。我试图将表单验证移动到下面的if else语句,它不显示验证消息,但无法将数据提交到数据库。下面是我的控制器功能。

 public function Insert_Result($Course_ID=null)
     {
         $this-load-helper('form');
         $this-load-library('form_validation');

         /* Model */
         $this-load-model('ResultEvaluation');

         /* Session */
         $session_data = $this-session-userdata('logged_in');
         $data['Ins_ID'] = $session_data['Ins_ID'];
         $this-session-set_userdata($data);

         /* Form Validation*/

         //$this-form_validation-set_rules('Course_ID', 'Course_ID', 'required');
         $this-form_validation-set_rules('Matric_No', 'Matric_No','required');
         $this-form_validation-set_rules('Student_Name', 'Student_Name', 'required');
         $this-form_validation-set_rules('Result_Mark_1', 'Result_Mark_1', 'required');
         $this-form_validation-set_rules('Result_Mark_2', 'Result_Mark_2', 'required');
         $this-form_validation-set_rules('Result_Mark_3', 'Result_Mark_3', 'required');
         $this-form_validation-set_rules('Result_Mark_4', 'Result_Mark_4', 'required');
         $this-form_validation-set_rules('Result_Mark_5', 'Result_Mark_5', 'required');

         if ($this-form_validation-run() === FALSE)
         {
             $data['results'] = $this-ResultEvaluation-get_record();
             $data['query'] = $this-ResultEvaluation-view($Course_ID);

             $this-load-view('templates/header');
             $this-load-view('Insert_Result', $data);
             $this-load-view('templates/footer');
         }
         else
         {
             $my_action = $this-input-post('submit');
             if ($my_action == 'Submit') 
             {
                 $this-ResultEvaluation-insert_record($Course_ID);
                 redirect('Result_Evaluation/Student_Result_List/'.$Course_ID,
 'refresh'); 
             }
         }        
         $my_action = $this-input-post('submit');
         if ($my_action == 'Cancel') 
         {
             redirect('Result_Evaluation/Student_Result_List/'.$Course_ID,
 'refresh');
         }

      }

1 个答案:

答案 0 :(得分:0)

我认为这应该有帮助

public function index(){
        $this->load->library(array('form_validation'));
        $this->form_validation->set_rules('field_name', 'Text','trim|required');

            if($this->form_validation->run()==TRUE){

               // do something
            }else{
                // validation error
                redirect("to/your/controller");
                break;
            }
    }

有关详细信息,请访问:http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html