Codeigniter将2个参数传递给回调

pig*_*fox 25 parameters codeigniter callback

发布一个名为'id'和'url'的两个字段的表单后,我有以下代码:

$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');
Run Code Online (Sandbox Code Playgroud)

db查询需要两个字段.

函数url_check($ str,$ id)被调用,但在这种情况下,'id'的值始终为0.

如果我这样做:

$this->form_validation->set_rules('url', 'url', 'trim|xss_clean|callback_url_check');
Run Code Online (Sandbox Code Playgroud)

并呼吁url_check($str)一切正常,因为它应该做.

问题是如何将两个值传递给url_check($str, $id)

Tee*_*eej 47

您可以直接使用$ this-> input-> post:

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;
}
Run Code Online (Sandbox Code Playgroud)


Phi*_*ipp 32

按照文档中的描述,以正确的方式(至少对于CI 2.1+)这样做:

$this->form_validation->set_rules('uri', 'URI', 'callback_check_uri['.$this->input->post('id').']');
// Later:
function check_uri($field, $id){
    // your callback code here
}
Run Code Online (Sandbox Code Playgroud)

  • 那就是我在帖子中提到它的原因;) (4认同)

小智 8

这似乎也有效.

$id = 1;

$this->form_validation->set_rules('username', 'Human Username', 'callback_username_check['.$id.']');

function username_check($str, $id) {
    echo $id;
    if ($str == 'test') {
         $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
         return FALSE;
    }
    else {
    return TRUE;
    }
}
Run Code Online (Sandbox Code Playgroud)