Codeigniter passing 2 arguments to callback

2019-01-11 07:18发布

问题:

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)?

回答1:

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


回答2:

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
}


回答3:

This seems to work also.

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


回答4:

If I understand form_validation correctly, each rule (set_rules) is for one field of the form and your callback will only check the one field. In your case it would seem that 'id' is out of scope. Instead, one can pass an array to the set_rules function and do the callback. I have not tried this yet. http://codeigniter.com/user_guide/libraries/form_validation.html#validationrulesasarray



回答5:

Just a note on using the callback parameters as suggested in the other answers. If you are using app/config/form_validation.php to create your validation rules, the $this->input->post('parameter') syntax will not work since that object is not available in the CI Loader at the point in the execution where it reads the contents of that file. You would have to do the call in your callback routine e.g. :

public function _validate_user_signup($username, $password) {
  $var = $this->input->post('password');

In this case the second parameter passed to the method would not contain the password, but $var would after the call.

I hope that's clear. Matt



回答6:

It's better to use Form Validation library to get the data that is being validated.

Not always your data will be in $_GET or $_POST (see https://www.codeigniter.com/userguide3/libraries/form_validation.html#validating-an-array-other-than-post).

The best way you can access your data inside a validation callback is this:

$this->form_validation->validation_data

"validation_data" is a public property in the CI_Form_validation class.