How to make is_unique two fields?

Asked

Viewed 195 times

2

I’m in a need I don’t know how to accomplish.

Well, my problem is:

I have a field is_unique which refers to two fields of my BD, the IDENTIFIER and the PLAN. I would like to know how to carry out this validation?

for a single field I use so:

$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 can’t do it with two single fields.

  • Ever tried to make "|is_unique[campo1]|is_unique[campo2]" ?

  • Worse than ever... and nothing.

  • See this answer http://stackoverflow.com/questions/14575165/validation-unique-field-in-codeigniter-with-2-index

  • 2

    Thank you, now I understand how to perform the validation.

1 answer

1

I’ll leave translated the link that @Wallace Masters reported above for those who don’t know English.

"I don’t think the IC has an internal solution for case with more than one Primary Key. I would use acallback_ like this:

But note that you need to send the second primary key as extra information and the rule needs to be applied in 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;}

        }

Addition of mine:

Callbacks - Using your own validation ( CI 3.0 )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.