我正在尝试使用 CodeIgniters 表单验证库,尽管我的验证规则似乎有效,但每当我调用 validation_errors() 时,我都会得到一个空字符串。

这是一个代码片段。

$base_rules = 'required|trim';

$this->_validation_rules = array(
        array(
            'field' => 'name',
            'label' => 'name',
            'rules' => $base_rules . '|alpha_numeric|min_length[5]|max_length[30]'
        ),
        array(
            'field' => 'price',
            'label' => 'Price',
            'rules' => $base_rules . '|decimal'
        ),
        array(
            'field' => 'duration',
            'label' => 'Duration',
            'rules' => $base_rules . '|integer'
        ),
        array(
            'field' => 'booking',
            'label' => 'Booking',
            'rules' => $base_rules . '|integer'
        )
    );
    $this->form_validation->set_rules($this->_validation_rules);

    if ($this->form_validation->run()) {
        // do stuff
    }else{
        // prints an empty string
         var_dump(validation_errors());
         exit;
    }

有谁知道为什么会这样,以及我如何得到我的错误?

最佳答案

将 validation_errors() 放在表单顶部,并将代码更改为

 if ($this->form_validation->run()) {
        // do stuff
    }else{
       $this->load->view('myform'); //display your form again
    }

validation_errors() 只会在 View 中填充。

关于php - CodeIgniter 表单验证,无法获取验证错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14656936/

10-11 23:34
查看更多