Codeigniter - 显示数组字段的单独错误消息

发布于 2024-12-20 12:31:37 字数 449 浏览 4 评论 0原文

拥有大量客户,每个客户都有各自的详细信息。这是一个非常简单的例子。

在 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 技术交流群。

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

发布评论

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

评论(2

已下线请稍等 2024-12-27 12:31:37

查看表单验证文档,特别是使用数组作为字段名称部分,我认为您需要明确命名您的输入
通过在名称中包含索引以使 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 name customer_names[0].

挽梦忆笙歌 2024-12-27 12:31:37

我在使用 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]');
...

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