使用Codeigniter form_validation回调文件输入解析参数

时间:2012-08-22 11:55:29

标签: codeigniter file-upload validation

我有一个上传输入,并试图通过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 Userguide表示在调用函数时,第一个参数被解析为函数内的第二个参数。

两个参数都没有被解析。我在Codeigniter Forum上发现了这篇文章 这似乎可以解释发生了什么(变量被剥离)。如果我改为<input type="text" /> params工作...... 有没有解决这个问题?

4 个答案:

答案 0 :(得分:0)

您需要像这样编辑代码:

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

我还注意到你在form_validation-&gt; set_rules中没有传递任何id值,所以在你的函数中你应该这样做:

public function upload_check($str, $id=0){..}

答案 1 :(得分: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;
    }
}

答案 2 :(得分:0)

我知道这是一个老问题,但我遇到了同样的问题,我终于意识到第二个参数会以引号返回,所以如果你传递$id的值为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)
}

答案 3 :(得分: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
    
    }