Validation UNIQUE field in Codeigniter with 2 index

前端 未结 4 529
不思量自难忘°
不思量自难忘° 2021-01-18 08:07

In the Codeigniter Framework, I can validate an Unique field in the MYSQL Database using the \"Form Validation Class\". Exemple:

$this->form_validation-&         


        
4条回答
  •  悲&欢浪女
    2021-01-18 08:39

    I don't think that CI has built-in case for combined PK but I would use callback_ like this: but note that you have to send the second PK as extra and the rule should be applied on the first $PK see callbacks for more info about that

    $this->form_validation->set_rules('form_field', 'form_label', 'callback_combpk[$pk2]');
        public function combpk($pk1, $pk2)
            {
                   $this->db->where('field1', $pk1);
                   $this->db->where('field2', $pk2);
                   $result = $this->db->get('table');
                   if($result->num_rows() > 0)
                   {
                      $this->form_validation->set_message('combpk','something'); // set your message
                      return false;
                   }
                   else{ return true;}
    
            }
    

提交回复
热议问题