CodeIgniter form_validation-> run()无效

时间:2016-04-06 12:05:10

标签: php codeigniter

我是CodeIgniter的新手,我一直在尝试实现表单提交功能,但每当我按“提交”表单页面时,只需刷新并且数据库不会更新!似乎$ this-> form_validation-> run()没有执行,但我不知道为什么

这是我的控制器代码

public function Tax(){
    $user_id=$this->session->userdata('user_id');
    $GetLastIDArr = $this->Tax_model->GetLastID();
    $GetLastID=$GetLastIDArr['AUTO_INCREMENT'];
    $this->data['DataTax']=$this->Tax_model->get_tax($user_id);

    /*-----Get List of Id's-----*/
    $Key_Tax_IDs=NULL;
    while($row = each($this->data['DataTax'])){
        $Key_Tax_IDs[]=$row[1]['tax_id'];
    }
    /*-----Get List of Attachments-----*/
    $TempFilePaths=$this->Tax_model->get_file_paths($Key_Tax_IDs,$user_id);
    $DataFilePaths=array();
    foreach($TempFilePaths as $key=>$TempValue){
        $DataFilePaths[$TempValue['field_name']][$TempValue['entry_id']]=$TempValue;
    }
    $this->data['DataFilePaths']=$DataFilePaths;

    if($this->input->post('tax')){

        $this->load->library('form_validation');
        foreach($this->input->post('key_id') as $key =>$value ){

            $this->form_validation->set_rules('key_id['.$key.']', 'Key ID '. ($key+1), 'required');
            $this->form_validation->set_rules('Birth_Certificate_number['.$key.']', 'Birth Certificate Number '. ($key+1), 'required');
            $this->form_validation->set_rules('pasport_number['.$key.']', 'Passport Number '. ($key+1), 'required');            
            $this->form_validation->set_rules('Marriage_Certificate_number['.$key.']', 'Marraige Certificate Number '. ($key+1), 'required');
            $this->form_validation->set_rules('drivers_Licence_Number['.$key.']', 'Driver License '. ($key+1), 'required'); 
            $this->form_validation->set_rules('medicarenumber['.$key.']', 'Medicare Number'. ($key+1), 'required'); 
            $this->form_validation->set_rules('medicarevalid['.$key.']', 'Medicare Valid to'. ($key+1), 'required');    
            $this->form_validation->set_rules('medicarefamilyno['.$key.']', 'Medicare Family No'. ($key+1), 'required');    
            $this->form_validation->set_rules('ambulanceno['.$key.']', 'Ambulance No'. ($key+1), 'required');   
            $this->form_validation->set_rules('ambulancevalid['.$key.']', 'Ambulance Valid to'. ($key+1), 'required');  
            $this->form_validation->set_rules('advisor['.$key.']', 'Advisor '. ($key+1), 'required');       
            $this->form_validation->set_rules('POA_advisor['.$key.']', 'POA Advisor '. ($key+1), 'required');       
            $this->form_validation->set_rules('AHD_advisor['.$key.']', 'AHD Advisor '. ($key+1), 'required');
            $this->form_validation->set_rules('TAX_advisor['.$key.']', 'TAX Advisor '. ($key+1), 'required');   
            $this->form_validation->set_rules('will_advisor['.$key.']', 'Will Advisor '. ($key+1), 'required');
            $this->form_validation->set_rules('Birth_Certificate_Storage_id['.$key.']', 'Birth Certificate Storage'. ($key+1), 'required');
            $this->form_validation->set_rules('Marriage_Certificate_Storage_ID['.$key.']', 'Marriage Certificate Storage'. ($key+1), 'required');
            $this->form_validation->set_rules('Passport_Storage['.$key.']', 'Passport_Storage'. ($key+1), 'required');
            $this->form_validation->set_rules('will_Storage['.$key.']', 'will Storage'. ($key+1), 'required');
            $this->form_validation->set_rules('POA_Storage['.$key.']', 'POA Storage'. ($key+1), 'required');
            $this->form_validation->set_rules('AHD_Storage['.$key.']', 'AHD Storage'. ($key+1), 'required');
            $this->form_validation->set_rules('Tax_Storage['.$key.']', 'Tax Storage'. ($key+1), 'required');
            $this->form_validation->set_rules('organ_Storage['.$key.']', 'organ Storage'. ($key+1), 'required');
        }
        $this->form_validation->set_rules('Importance_JK', 'Importance', 'required');
        //upload path
        $uploadpath = TAX_IMG_PATH."/".$user_id;
        mkdir($uploadpath,0777, true);

        if ($this->form_validation->run() == TRUE){
            $config = array(
                'upload_path'   => $uploadpath,
                'allowed_types' => 'jpg|gif|png|pdf|docx|doc',
                'overwrite'     => TRUE,
            );
            $this->load->library('upload', $config);

            $tax = $this->input->post();
            $TaxInsert=array();
            $TaxUpdate=array();
            $Key_Tax__ExistIDs=array();

            $ImgInsert=array();
            $ImgUpdate=array();
            $files=$_FILES;
            $imgkey=0;

            foreach($this->input->post('key_id') as $key =>$value ){

                $TaxInsert[$key]['updated'] = Date("Y-m-d");
                $TaxInsert[$key]['Importance_JK'] = $tax['Importance_JK'];
                $TaxInsert[$key]['user_FK'] = $user_id;
                $TaxInsert[$key]['Person_id'] = $tax['key_id'][$key];
                $TaxInsert[$key]['Birth_Certificate_number'] = $tax['Birth_Certificate_number'][$key];
                $TaxInsert[$key]['pasport_number'] = $tax['pasport_number'][$key];
                $TaxInsert[$key]['Marriage_Certificate_number'] = $tax['Marriage_Certificate_number'][$key];
                $TaxInsert[$key]['drivers_Licence_Number'] = $tax['drivers_Licence_Number'][$key];
                $TaxInsert[$key]['medicarenumber'] = $tax['medicarenumber'][$key];
                $TaxInsert[$key]['medicarevalid'] = ($tax['medicarevalid'][$key] !="")?Date("Y-m-d",strtotime(str_replace("/","-",$tax['medicarevalid'][$key]))):NULL;
                $TaxInsert[$key]['medicarefamilyno'] = $tax['medicarefamilyno'][$key];
                $TaxInsert[$key]['ambulanceno'] = $tax['ambulanceno'][$key];
                $TaxInsert[$key]['ambulancevalid'] = ($tax['ambulancevalid'][$key] !="")?Date("Y-m-d",strtotime(str_replace("/","-",$tax['ambulancevalid'][$key]))):NULL;
                $TaxInsert[$key]['Birth_Certificate_Storage_id'] = $tax['Birth_Certificate_Storage_id'][$key];
                $TaxInsert[$key]['Marriage_Certificate_Storage_ID'] = $tax['Marriage_Certificate_Storage_ID'][$key];
                $TaxInsert[$key]['passport_Expires'] =($tax['passport_Expires'][$key] !="")?Date("Y-m-d",strtotime(str_replace("/","-",$tax['passport_Expires'][$key]))):NULL;
                $TaxInsert[$key]['DL_Expires'] = ($tax['DL_Expires'][$key] !="")?Date("Y-m-d",strtotime(str_replace("/","-",$tax['DL_Expires'][$key]))):NULL;
                $TaxInsert[$key]['religion'] = $tax['religion'][$key];
                $TaxInsert[$key]['Lodge_tax'] = $tax['Lodge_tax'][$key];
                $TaxInsert[$key]['will'] = $tax['will'][$key];
                $TaxInsert[$key]['POA'] = $tax['POA'][$key];
                $TaxInsert[$key]['Advance_Health_Directive'] = $tax['Advance_Health_Directive'][$key];
                $TaxInsert[$key]['tax_file'] = $tax['tax_file'][$key];
                $TaxInsert[$key]['any_part'] = $tax['any_part'][$key];
                $TaxInsert[$key]['kidney'] = $tax['kidney'][$key];
                $TaxInsert[$key]['organdonor'] = $tax['organdonor'][$key];
                $TaxInsert[$key]['POA_advisor'] = $tax['POA_advisor'][$key];
                $TaxInsert[$key]['AHD_advisor'] = $tax['AHD_advisor'][$key];
                $TaxInsert[$key]['TAX_advisor'] = $tax['TAX_advisor'][$key];
                $TaxInsert[$key]['will_advisor'] = $tax['will_advisor'][$key];
                $TaxInsert[$key]['advisor'] = $tax['advisor'][$key];
                $TaxInsert[$key]['will_Date'] =($tax['will_Date'][$key] !="")?Date("Y-m-d",strtotime(str_replace("/","-",$tax['will_Date'][$key]))):NULL;
                $TaxInsert[$key]['POA_Date'] =($tax['POA_Date'][$key] !="")?Date("Y-m-d",strtotime(str_replace("/","-",$tax['POA_Date'][$key]))):NULL;
                $TaxInsert[$key]['AHD_date'] =($tax['AHD_date'][$key] !="")?Date("Y-m-d",strtotime(str_replace("/","-",$tax['AHD_date'][$key]))):NULL;
                $TaxInsert[$key]['Tax_date'] =($tax['Tax_date'][$key] !="")?Date("Y-m-d",strtotime(str_replace("/","-",$tax['Tax_date'][$key]))):NULL;
                $TaxInsert[$key]['heart'] = $tax['heart'][$key];
                $TaxInsert[$key]['class'] = $tax['class'][$key];
                $TaxInsert[$key]['will_Storage'] = $tax['will_Storage'][$key];
                $TaxInsert[$key]['POA_Storage'] = $tax['POA_Storage'][$key];
                $TaxInsert[$key]['AHD_Storage'] = $tax['AHD_Storage'][$key];
                $TaxInsert[$key]['Tax_Storage'] = $tax['Tax_Storage'][$key];
                $TaxInsert[$key]['organ_Storage'] = $tax['organ_Storage'][$key];
                $TaxInsert[$key]['Passport_Storage'] = $tax['Passport_Storage'][$key];
                if(isset($tax['id'][$key])){
                    $Key_Tax__ExistIDs[]=$tax['id'][$key];
                    $TaxUpdate[$key]=$TaxInsert[$key];
                    $TaxUpdate[$key]['tax_id']=$tax['id'][$key];
                    unset($TaxInsert[$key]);
                }else{
                    $TaxInsert[$key]['tax_id'] = $GetLastID;
                    $GetLastID++;
                }
            }
            $idsToDelete='';
            if(empty($TaxInsert) &&  empty($TaxUpdate)){
                $idsToDelete=array_diff($Key_Tax_IDs,$Key_Tax__ExistIDs);
            }
            $status=$this->Tax_model->ProcessData($idsToDelete,$TaxUpdate,$user_id,$TaxInsert,$ImgInsert,$ImgUpdate);
            redirect('Tax','refresh');
        }else{
            $tax = $this->input->post();
            foreach($this->input->post('key_id') as $key =>$value ){                        
                $TaxErr[$key]['updated'] = Date("Y-m-d");
                $TaxErr[$key]['Importance_JK'] = $tax['Importance_JK'];
                $TaxErr[$key]['Person_id'] = $tax['key_id'][$key];
                $TaxErr[$key]['Birth_Certificate_number'] = $tax['Birth_Certificate_number'][$key];
                $TaxErr[$key]['pasport_number'] = $tax['pasport_number'][$key];
                $TaxErr[$key]['Marriage_Certificate_number'] = $tax['Marriage_Certificate_number'][$key];
                $TaxErr[$key]['drivers_Licence_Number'] = $tax['drivers_Licence_Number'][$key];
                $TaxErr[$key]['medicarenumber'] = $tax['medicarenumber'][$key];
                $TaxErr[$key]['medicarevalid'] = ($tax['medicarevalid'][$key] !="")?Date("Y-m-d",strtotime(str_replace("/","-",$tax['medicarevalid'][$key]))):NULL;
                $TaxErr[$key]['medicarefamilyno'] = $tax['medicarefamilyno'][$key];
                $TaxErr[$key]['ambulanceno'] = $tax['ambulanceno'][$key];
                $TaxErr[$key]['ambulancevalid'] = ($tax['ambulancevalid'][$key] !="")?Date("Y-m-d",strtotime(str_replace("/","-",$tax['ambulancevalid'][$key]))):NULL;
                $TaxErr[$key]['Birth_Certificate_Storage_id'] = $tax['Birth_Certificate_Storage_id'][$key];
                $TaxErr[$key]['Marriage_Certificate_Storage_ID'] = $tax['Marriage_Certificate_Storage_ID'][$key];
                $TaxErr[$key]['passport_Expires'] = ($tax['passport_Expires'][$key] !="")?Date("Y-m-d",strtotime(str_replace("/","-",$tax['passport_Expires'][$key]))):NULL;
                $TaxErr[$key]['DL_Expires'] = ($tax['DL_Expires'][$key] !="")?Date("Y-m-d",strtotime(str_replace("/","-",$tax['DL_Expires'][$key]))):NULL;
                $TaxErr[$key]['religion'] = $tax['religion'][$key];
                $TaxErr[$key]['Lodge_tax'] = $tax['Lodge_tax'][$key];
                $TaxErr[$key]['will'] = $tax['will'][$key];
                $TaxErr[$key]['POA'] = $tax['POA'][$key];
                $TaxErr[$key]['Advance_Health_Directive'] = $tax['Advance_Health_Directive'][$key];
                $TaxErr[$key]['tax_file'] = $tax['tax_file'][$key];
                $TaxErr[$key]['any_part'] = $tax['any_part'][$key];
                $TaxErr[$key]['kidney'] = $tax['kidney'][$key];
                $TaxErr[$key]['organdonor'] = $tax['organdonor'][$key];
                $TaxErr[$key]['POA_advisor'] = $tax['POA_advisor'][$key];
                $TaxErr[$key]['AHD_advisor'] = $tax['AHD_advisor'][$key];
                $TaxErr[$key]['TAX_advisor'] = $tax['TAX_advisor'][$key];
                $TaxErr[$key]['will_advisor'] = $tax['will_advisor'][$key];
                $TaxErr[$key]['advisor'] = $tax['advisor'][$key];
                $TaxErr[$key]['will_Date'] = ($tax['will_Date'][$key] !="")?Date("Y-m-d",strtotime(str_replace("/","-",$tax['will_Date'][$key]))):NULL;
                $TaxErr[$key]['POA_Date'] = ($tax['POA_Date'][$key] !="")?Date("Y-m-d",strtotime(str_replace("/","-",$tax['POA_Date'][$key]))):NULL;
                $TaxErr[$key]['AHD_date'] = ($tax['AHD_date'][$key] !="")?Date("Y-m-d",strtotime(str_replace("/","-",$tax['AHD_date'][$key]))):NULL;
                $TaxErr[$key]['Tax_date'] = ($tax['Tax_date'][$key] !="")?Date("Y-m-d",strtotime(str_replace("/","-",$tax['Tax_date'][$key]))):NULL;
                $TaxErr[$key]['heart'] = $tax['heart'][$key];
                $TaxErr[$key]['class'] = $tax['class'][$key];
                $TaxErr[$key]['will_Storage'] = $tax['will_Storage'][$key];
                $TaxErr[$key]['POA_Storage'] = $tax['POA_Storage'][$key];
                $TaxErr[$key]['AHD_Storage'] = $tax['AHD_Storage'][$key];
                $TaxErr[$key]['Tax_Storage'] = $tax['Tax_Storage'][$key];
                $TaxErr[$key]['organ_Storage'] = $tax['organ_Storage'][$key];
                $TaxErr[$key]['Passport_Storage'] = $tax['Passport_Storage'][$key];
                if(isset($tax['id'][$key])){
                    $TaxErr[$key]['tax_id']=$tax['id'][$key];
                }
                $this->data['DataTax']=$TaxErr;
                $this->data['validation_errors']= validation_errors();
            }
        }
    }   
    }
    Problem Which i'm currently facing is,im not able to save form
    Any help will be much appreciated

1 个答案:

答案 0 :(得分:1)

您的控制器非常复杂。但是第一眼看到尝试坚持用户指南中提出的模式并改进:

$this->load->library('form_validation');
$this->form_validation->set_rules('key_id['.$key.']', 'Key ID '. ($key+1), 'required'); //etc

        if ($this->form_validation->run() == FALSE)
        {
                $this->load->view('myform');
        }
        else
        {
                //process form, here $this->input->post() goes
                $this->load->view('formsuccess');
        }

还要检查表单操作在视图文件中的位置。在建议的这种控制器中,你只需将其留空。

echo form_open('');