0

我有一个上传输入,并试图通过 CI form_validation 库解析回调函数的参数。

$this->form_validation->set_rules('orderfile', 'Order Form'," trim|callback_upload_check[$account_id]");

这调用:

public function upload_check($str, $id)
{
    $errors = $this->do_upload($id);

    if(isset($errors['error']))
    {
        $this->form_validation->set_message('upload_check', $errors['error']);

        return FALSE;
    }else{
        return TRUE;
    }
}

Codeigniter用户指南指出,在调用函数时,第一个参数被解析为函数内的第二个参数。

两个参数都没有被解析。我在Codeigniter 论坛上找到了这篇文章 这似乎解释了正在发生的事情(变量被剥离)。如果我将<input type="text" />参数更改为工作...有没有办法解决这个问题?

4

4 回答 4

0

我知道这是一个老问题,但我遇到了同样的问题,我终于意识到第二个参数用引号括起来,所以如果你传递$id带有 value的 an 1,它实际上返回为"1".

因此,对于原始问题,您需要像这样回调函数:

$this->form_validation->set_rules('orderfile', 'Order Form'," trim|callback_upload_check[".$account_id."]");

在你的回调函数中:

public function upload_check($str, $id){
  $actual_id=str_replace('"', "", $id)
}
于 2013-01-20T13:20:16.410 回答
0

您需要将功能更改为:

public function upload_check($orderfile)
{
    $errors = $this->do_upload($orderfile);

    if(isset($errors['error']))
    {
        $this->form_validation->set_message('upload_check', $errors['error']);

        return FALSE;
    }else{
        return TRUE;
    }
}
于 2012-08-22T18:41:40.977 回答
0

你需要像这样编辑你的代码:

$this->form_validation->set_rules('orderfile', 'Order Form'," trim|callback_upload_check[".$account_id."]");

我还注意到,在您的 form_validation->set_rules 中,您没有为 id 传递任何值,因此在您的函数中您应该这样做:

public function upload_check($str, $id=0){..}
于 2012-08-22T12:20:39.930 回答
0
    $config =array(
       
        array(
            "field" => "userEmail",
            "label" => ":userEmail:",
            "rules" => "required|valid_email",
        ),
        array(
            "field" => "userPassword",
            "label" => ":userPassword:",
            "rules" => "required|min_length[8]",
        ),
        
    );

    $error_messages = array(
        "required" => "{field} the field is required.",
        "min_length" => "{field} the field value is so short",
        "valid_email" => "{field} please valid email",
    );

    $this->form_validation->set_message($error_messages);
    $this->form_validation->set_rules($config);

    if($this->form_validation->run() == FALSE) {
        $alert =preg_replace("/(\n)+/m", ' ', strip_tags(validation_errors()));
        $explode =explode(':', $alert);
        
        $arr =array();
        for($i=1; $i < count($explode); $i+=2){
            $y=$i;
            $j =++$y;
            $arr[$explode[$i]] = $explode[$j];
        }
        print json_encode($arr);
        
    } else { 
            //process
    
    }
于 2021-01-28T14:01:38.973 回答