简体   繁体   English

如何在Codeigniter表单验证中获取字段数组的索引?

[英]How can I get the index of a field array in codeigniter form validation?

I am building a codeigniter data entry form. 我正在建立一个codeigniter数据输入表单。 The form is however mostly dynamically generated in JS based on how many entries need to be submitted. 但是,该表单主要是根据需要提交的条目数在JS中动态生成的。

This means I have to use 这意味着我必须使用

<input type='text' name='invoice[]' id='invoice" + index + "' class='form-control' size='20' value=''>

To validate it I use 为了验证我使用

$this->form_validation->set_rules('invoice[]', 'Invoice field', 'required|trim|alpha_numeric|is_unique[BillingInvoices.InvoiceNumber]',
            array('required' => 'Must enter invoice number.',
                'alpha_numeric' => 'Invoice can only contain letters and numbers',
                'is_unique' => 'Invoice number must be unique in database.')
        );

My problem now is that I have no idea how to get the index of the entry that triggers that rule. 我现在的问题是我不知道如何获取触发该规则的条目的索引。 My clients need to be able to know which invoice is causing the problem. 我的客户需要能够知道引起问题的发票。

I would validate it through javascript too. 我也将通过javascript对其进行验证。 Most of the major browsers support javascript so almost all of your users will get the error before they submit the form. 大多数主要的浏览器都支持javascript,因此几乎所有用户都将在提交表单之前收到错误消息。

This is my proposal: 这是我的建议:

Step 1: Setup any javascript validation library like validate.js (or any other) in your project. 步骤1:在专案中建立任何JavaScript验证程式库,例如validate.js (或其他任何程式)。

Step 2: Configure the javascript validation so your users get the error before they submit the form. 步骤2:配置javascript验证,以便您的用户在提交表单之前收到错误消息。 They will get the error on the affected input. 他们将在受影响的输入上得到错误。

Step 3: Keep the PHP validation. 步骤3:保留PHP验证。 This is important because javascript can be disabled or even hacked, so we always need to validate user information at the server. 这很重要,因为javascript可以被禁用甚至被黑客入侵,因此我们始终需要在服务器上验证用户信息。

This way you will cover your problem: users will have the error message at the form and your database will always have right information. 这样,您将解决问题:用户将在表单上看到错误消息,并且数据库将始终具有正确的信息。

Hope this helps, greetings. 希望这会有所帮助,问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM