How to do is_unique of two fields?

2

I have a need that I do not know how to accomplish.

Well, my problem:

I have a is_unique field that refers to two fields in my DB, IDENTIFIER, and PLANT. Would you like to know how to perform this validation?

For a single field I use this way:

$imeiRules = "required|min_length[3]|max_length[45]|xss_clean|strip_tags";
        if($this->input->post('txtImei') != $this->input->post('imeiOld')){
            $imeiRules .= " |is_unique[equipamento.equipamento_imei]";
        }

I just do not know what to do with single fields.

    
asked by anonymous 10.07.2015 / 13:42

1 answer

1

I will leave the link that @Wallace Masters informed above for those who do not know English.

"I do not think CI has an internal solution for a case with more than one Primary Key. I would use a callback_ like this:

But notice that you need to send the second primary key as an extra information and the rule needs to be applied to the first primary key.

Search the documentation for callbacks to learn more. "

$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;}

        }

My addition:

Callbacks - Using Your Own Validation ( CI 3.0)

    
27.04.2016 / 07:00