gpt4 book ai didi

php - CodeIgniter 表单验证,无法获取验证错误

转载 作者:可可西里 更新时间:2023-10-31 22:12:57 26 4
gpt4 key购买 nike

我正在尝试使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com