Codeigniter - 显示数组字段的单独错误消息
拥有大量客户,每个客户都有各自的详细信息。这是一个非常简单的例子。
在 codeigniter 中,每个 customer_name 都是必需的$this->form_validation->set_rules('customer_names[]','Customer Names','required');
如果任何客户名称为空,validation_errors();
显示整个数组的一条消息。
我如何获取该客户的单独错误消息?
注意: echo form_error('customer_names[0]');
是我试图实现的目标,其中 customer_name 0 留空。
Have array of customers each with individual details. Here is a very SIMPLE example.
<input type="text" name="customer_names[]" />
In codeigniter, each customer_name is required$this->form_validation->set_rules('customer_names[]','Customer Names','required');
If any of the customer names are blank, validation_errors();
shows one message for the entire array.
How can I obtain individual error messages for that customer?
NOTE: echo form_error('customer_names[0]');
is what I am trying to achieve where customer_name 0 was left blank.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看表单验证文档,特别是使用数组作为字段名称部分,我认为您需要明确命名您的输入
通过在名称中包含索引以使
form_error()
方法按您的意愿工作。因此,为了让
form_error('customer_names[0]')
正常工作,实际上必须有一个名为customer_names[0]
的输入。Looking at the Form Validation documentation, specifically the Using Arrays as Field Names section, I think you're going to need to explicitly name your inputs
by including the index in the name to get the
form_error()
method to work as you desire.So in order for
form_error('customer_names[0]')
to work, there will actually have to be an input with the namecustomer_names[0]
.我在使用 CodeIgniter 2.1.3 时遇到了同样的问题。我已经这样解决了:
输入是:
...
表单验证为:
$this->form_validation->set_rules('customer_names[0]','Customer Names','required');
$this->form_validation->set_rules('customer_names[1]','客户姓名','必填');
...
错误显示如下:
echo form_error('customer_names[0]');
echo form_error('customer_names[1]');
...
I was having the same problem with CodeIgniter 2.1.3. I have resolved it like this:
The input is:
<input type="text" name="customer_names[0]" />
<input type="text" name="customer_names[1]" />
...
The form validation is:
$this->form_validation->set_rules('customer_names[0]','Customer Names','required');
$this->form_validation->set_rules('customer_names[1]','Customer Names','required');
...
The errors are displayed like this:
echo form_error('customer_names[0]');
echo form_error('customer_names[1]');
...