Codeigniter表单验证:运行函数不起作用


Codeigniter form validation: run function is not working

我正在使用代码点火器中的from,程序控制正在移动到提交函数,我可以通过添加die函数来测试该函数。尽管set_rules()成功地检查了条目,但控件没有传递给if($this->form_validation->run())这个函数。它退出并运行我一直用来测试程序流的die函数dead-2。

下面是我的控制器代码

function addPost(){
$this->load->library('form_validation')
if($this->admin_lib->checkMembers()){
if($this->input->post('submit')){
   //validate the form
$this->form_validation->set_rules('country','Country','required');
$this->form_validation->set_rules('city','City','required');
$this->form_validation->set_rules('area','Area','required');
$this->form_validation->set_rules('street','Street','required');
$this->form_validation->set_rules('house_no','House number','required|numeric');
if($this->form_validation->run()){
   //add to database
   die("dead-1");
    if($this->members_model->addPost())
    {
    echo "Successfully made one entry will be validated";
    }
    else{
        echo "Error uploading the datas into database Please contact us about the problem";
            }
        }
   die("Dead -2");
    }
    $data['content']=$this->load->view('members/addPost','',true);
    $this->load->view('members/home',$data);
    }
    else{
    echo "you dont have preveledge to access this page ,<br/> LOgin link rakhnu paryo ";
    }
   }

您的代码将始终调用die("Dead-2"),因为它不是Else块的一部分。它位于If语句的正下方,这意味着,无论表单验证发生什么,它都会消亡。

考虑将代码更改为以下

if($this->form_validation->run())
{
    //add to database
    die("dead-1");
    if($this->members_model->addPost())
    {
        echo "Successfully made one entry will be validated";
    }
    else
    {
        echo "Error uploading the datas into database Please contact us about the problem";
    }
}
else
{
    die("Dead -2");
}