CodeIgniter 验证错误 - 如何获取键数组 =>值对?
假设表单有错误,有没有办法获取键(字段名称)/值(错误消息)对的数组?例如:
['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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终扩展了核心类:
I ended up extending the core class:
这可能有点迟了,但我想分享一个我使用的解决方案,该解决方案不涉及扩展 CI 表单验证库。这个问题已经有一个公认的答案,但我希望它对其他人有益。
不幸的是,CodeIgniter 的
$this->form_validation->error_array()
方法仅返回set_rules()
方法产生的错误。它不考虑生成错误的表单字段的名称。但是,我们可以利用 CodeIgniter 的另一个方法$this->form_validation->error()
,它通过传递字段名称来返回与特定表单字段关联的错误作为参数。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 theset_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.