Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter passing 2 arguments to callback

After posting a form having two fields named 'id' and 'url' I have the following code:

$this->load->library('form_validation'); $this->form_validation->set_rules('id', 'id', 'trim|xss_clean'); $this->form_validation->set_rules('url', 'url|id', 'trim|xss_clean|callback_url_check'); 

A db query needs both fields.

The function url_check($str, $id) is called but in this case 'id' always has the value 0.

If I just do :

$this->form_validation->set_rules('url', 'url', 'trim|xss_clean|callback_url_check'); 

And call url_check($str) everything's working as it's is supposed to do.

The question is how do I pass two values to the url_check($str, $id)?

like image 976
pigfox Avatar asked Jan 27 '11 22:01

pigfox


2 Answers

You can use $this->input->post directly:

function check_url() {    $url = $this->input->post('url');    $id = $this->input->post('id');     // do some database things you need to do e.g.    if ($url_check = $this->user_model->check_url($url, $id) {        return TRUE;    }    $this->form_validation->set_message('Url check is invalid');    return FALSE; } 
like image 103
Teej Avatar answered Oct 04 '22 15:10

Teej


Just do it the right way (at least for CI 2.1+) as described in the docs:

$this->form_validation->set_rules('uri', 'URI', 'callback_check_uri['.$this->input->post('id').']'); // Later: function check_uri($field, $id){     // your callback code here } 
like image 28
Philipp Avatar answered Oct 04 '22 16:10

Philipp