0

I just want to know can we use this in CodeIgniter..

$this->form_validation->set_rules('marriage_id', 'Marriage ID','required|is_unique[marriage.marriage_id]|matches[person.marriage_id]');

marriage is my table name and marriage_id is a field in that table.

Person is another table and it also have marriage_id field.From above controller code, I'm inserting data into marriage table and i want to know that can I check, it matches with the marriage_id in person table or not.

Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
Kedar B
  • 774
  • 5
  • 18
  • 47
  • 1
    You probably need to use a [callback function](http://stackoverflow.com/questions/21439805/codeigniters-regex-match/21440204#21440204) as the custom validation method to search through the `Person` table for checking the existence of current `mariage_id`. – Hashem Qolami Feb 22 '14 at 09:38

2 Answers2

0

According to Codeigniter reference you can only user table.field with 'is_unique', but not with 'matches' (matches checks another form element, not db table).

You should use custom callback for check what you need, please read http://ellislab.com/codeigniter%20/user-guide/libraries/form_validation.html#callbacks

Robert Trzebiński
  • 1,327
  • 8
  • 16
0

No You cant. You can use table_name.feild_name with is_unique only

However u can write a callback function to check for your required validation

Something like this

$this->form_validation->set_rules('username', 'Username', 'callback_check_match');

and your custom call back function...

public function check_match($str)
{
     $result = $this->model_name->check_databse($str);
     //call to model function fo check string match in databse and return result
     if (return_result) {
          $this->form_validation->set_message('check_match', 'The %s field does not match ur desired feild');
          return FALSE;
     }
     else
     {
         return TRUE;
     }
}

In your Model :

public function check_databse($str){
 $this->db->select('count(*)',FALSE)->from('table_name')->where('coulmn_name',$str);
 retrun $this->db->get()->result_array();
}

For more reference check this link below for documentation on ci form validation library

http://ellislab.com/codeigniter%20/user-guide/libraries/form_validation.html

Mohan
  • 4,677
  • 7
  • 42
  • 65