In Codeigniter, how to pass a third parameter to a

2019-03-13 21:19发布

问题:

I am currently using the Form Validation class (on Codeigniter) and setting rules.

It works like this with two parameters (codeigniter.com/user_guide/libraries/form_validation.html):

$this->form_validation->set_rules('username', 'Username', 'callback_test[abc]');

But what about a third parameter? And a fourth...? Is it possible?

回答1:

It is not official but works

Split the parameter by ','

$this->form_validation->set_rules('article_big_image','Gambar Besar','callback_check_picture[article_big_image,edit]');

function check_picture($image,$param){
    $param = preg_split('/,/', $param);
    $field = $param[0];
    $action = $param[1];
    echo $field.$action;
}


回答2:

Not without extended the system form validation class. For information on how to achieve this take a look at this article.

Alternatively you can access the post variables within your callback function using:

$this->input->post('field_name');

which may or may not help you out.



回答3:

You can use Array for more parameters for more fields as like i did below:

 $error = array(

                array(
                'field' => 'check', // the field name will come here
                'label' => 'Check',
                'rules' => 'required|here you can put the callback Function'
                )
          );


回答4:


    $this->form_validation->set_rules('article_big_image','Gambar','callback_check_picture[article_big_image,edit]');
    function check_picture($image,$param){
        $param = explode(',', $param);
        $field = isset($param[0])?$param[0]:'';
        $action = isset($param[1])?$param[1]:'';
       echo $field.$action;
   }


回答5:

You can pass to the rule |callback_handle_is_unique_value_combinations[val1,val2]

than, public function callback_handle_is_unique_value_combinations($str, $field) { $fields = explode(',', $field); $val1 = $fields[0]; $val2 = $fields[1];

and then you made your cponarisons