CodeIgniter 验证错误 - 如何获取键数组 =>值对?

发布于 2025-01-07 04:34:56 字数 223 浏览 1 评论 0原文

假设表单有错误,有没有办法获取键(字段名称)/值(错误消息)对的数组?例如:

['name'] => 'The name field is required',
['age'] => 'The name must be greater than 18'

如果没有本机方法来执行此操作,我将扩展表单验证库并公开受保护的属性 *_error_array*。

Assuming a form with errors, is there a way to get an array of key (the field name) / value (the error message) pairs? For example:

['name'] => 'The name field is required',
['age'] => 'The name must be greater than 18'

If there is no native way to do this, I will extend the form validation library and expose the protected property *_error_array*.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恋你朝朝暮暮 2025-01-14 04:34:56

我最终扩展了核心类:

class MY_Form_validation extends CI_Form_validation
{
    public function error_array()
    {
        return $this->_error_array;
    }
}

I ended up extending the core class:

class MY_Form_validation extends CI_Form_validation
{
    public function error_array()
    {
        return $this->_error_array;
    }
}
神魇的王 2025-01-14 04:34:56

这可能有点迟了,但我想分享一个我使用的解决方案,该解决方案不涉及扩展 CI 表单验证库。这个问题已经有一个公认的答案,但我希望它对其他人有益。

不幸的是,CodeIgniter 的 $this->form_validation->error_array() 方法仅返回 set_rules() 方法产生的错误。它不考虑生成错误的表单字段的名称。但是,我们可以利用 CodeIgniter 的另一个方法 $this->form_validation->error(),它通过传递字段名称来返回与特定表单字段关联的错误作为参数。

//form validation rules
$this->form_validation->set_rules('f_name', 'First Name', 'trim|required', 
    ['required' => 'First Name is required']
);
$this->form_validation->set_rules('l_name', 'Last Name', 'trim|required', 
    ['required' => 'Last Name is required']
);
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]', 
    [
        'required'      => 'Email is required',
        'valid_email'   => 'Email format is invalid',
        'is_unique'     => 'Email already exists'
    ]
);
$error_arr = []; //array to hold the errors
//array of form field names
$field_names= ['f_name', 'l_name', 'email'];
//Iterate through the form fields to build a field=error associative pair.
foreach ($field_names as $field) {
    $error = $this->form_validation->error($field);
    //field has error?
    if (strlen($error)) $error_arr[$field] = $error;
}

//print_r($error_arr);

This might be way belated but I want to share a solution that I use that does not involve extending CI form validation library. The question already has an accepted answer but I hope that it will benefit someone else.

Unfortunately, CodeIgniter's $this->form_validation->error_array() method returns only the errors that emanate from the set_rules() method. It does not account for the name of the form field that generated the error. However, we can leverage another of CodeIgniter's method $this->form_validation->error(), which returns the error(s) associated with a particular form field, by passing the name of the field as parameter.

//form validation rules
$this->form_validation->set_rules('f_name', 'First Name', 'trim|required', 
    ['required' => 'First Name is required']
);
$this->form_validation->set_rules('l_name', 'Last Name', 'trim|required', 
    ['required' => 'Last Name is required']
);
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]', 
    [
        'required'      => 'Email is required',
        'valid_email'   => 'Email format is invalid',
        'is_unique'     => 'Email already exists'
    ]
);
$error_arr = []; //array to hold the errors
//array of form field names
$field_names= ['f_name', 'l_name', 'email'];
//Iterate through the form fields to build a field=error associative pair.
foreach ($field_names as $field) {
    $error = $this->form_validation->error($field);
    //field has error?
    if (strlen($error)) $error_arr[$field] = $error;
}

//print_r($error_arr);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文