如何使用Codeigniter中的where条件检查表单验证?

时间:2019-01-23 05:38:15

标签: codeigniter validation

enter image description here 在这张图片中,我必须验证Sno是否可用(即在具有2种类型(C或B)的类型字段中)。

例如在B型中有Sno.3,但C型中没有Sno。我必须根据类型验证Sno。

    public function Cash_Payment_Edit(){
        $session_data = $this->session->userdata('logged_in');
        $data['username'] = $session_data['username'];


$this->load->library('form_validation');

$this->form_validation->set_rules('Code', 'Sno', 'required|is_unique[pay.Sno]');    
 $this->form_validation->set_rules('C', 'type', 'required');

    $query = $this->db->get('parmaster');    

        $data['PName']=$query->result_array();


        $data['query1']=$this->User_model->viewAccMaster1();

         if ($this->form_validation->run() == FALSE) {
               $this->load->model('Export');
           $data['r'] = $this->Export->getcashpay();

        $this->load->view('BookKeeping/Cash_Payment_Edit',$data);
                }
                else
                {
            $this->session->set_flashdata('Add', 'The SNo is not in the database');
                redirect("BookKeeping/Cash_Payment_Entry","refresh");   
                }
    }

这是一个代码。

请帮助解决此问题。

1 个答案:

答案 0 :(得分:0)

这很容易做到。使用if()添加对Snotype的验证

$Sno = $this->input->post('Sno');
if(!empty($Sno)){

    //Add validation rule for 'type' when Sno not empty

    $this->form_validation->set_rules('Sno', 'Sno', 'required');
    $this->form_validation->set_rules('type', 'Type', 'required');

}else{
  //Add validation rule for 'type' when Sno is empty
} 

更多read the Doc