简体   繁体   English

关联数组的codeigniter 3验证

[英]codeigniter 3 validation of associative array

I am trying to validate an associative array pushed from my JavaScript, using the validation library. 我正在尝试使用验证库来验证从我的JavaScript推送的关联数组。

The following code is working except it is only validating (finding the values) for the last array within the associative array, is there a way for it to work for each instance of the array as it runs in the foreach? 下面的代码可以正常工作,只不过它仅对关联数组中的最后一个数组进行验证(查找值),在foreach中运行时,有没有办法让它对数组的每个实例起作用?

code: 码:

    if (!empty($JSON)) {
        foreach ($JSON AS $k => $data) {

            foreach ($data AS $key => $value) {
                $this->form_validation->set_data($data);
                if($key == 'itemCode' . $k){
                    $this->form_validation->set_rules($key, 'Item Code', 'required');
                }
                if($key == 'Desc' . $k){
                    $this->form_validation->set_rules($key, 'Description', 'required');
                }
                if($key == 'Qty' . $k){
                    $this->form_validation->set_rules($key, 'Quantity', 'required|numeric');
                }
                if($key == 'Cost' . $k){
                    $this->form_validation->set_rules($key, 'Cost', 'required|numeric');
                }

            }
            //$this->form_validation->reset_validation();

        }
    }

array output: 数组输出:

[0] => Array(
    [Counter0] => 0
[itemCode0] => 1
[Desc0] => 1
[Qty0] => 1
[Cost0] => 1
[Total0] => 1
)
[1] => Array(
    [Counter1] => 1
[itemCode1] => 2
[Desc1] => 2
[Qty1] => 2
[Cost1] => 2
[Total1] => 4
)
[2] => Array(
    [Counter2] => 2
[itemCode2] => 3
[Desc2] => 3
[Qty2] => 3
[Cost2] => 3
[Total2] => 9
)
[3] => Array(
    [Counter3] => 3
[itemCode3] => 4
[Desc3] => 4
[Qty3] => 4
[Cost3] => 4
[Total3] => 16
)

The problem is, the set_data function gets called between the set_rules function and according to CI 问题是,set_data函数在set_rules函数之间并根据CI被调用

You have to call the set_data() method before defining any validation rules. 您必须在定义任何验证规则之前调用set_data()方法。

For more information take a look at the documentation 有关更多信息,请参阅文档

A possible method would be to catch all data and rules in an array 一种可能的方法是捕获数组中的所有数据和规则

Below is an example how to achieve that, pls keep in mind i haven't tested it because i wrote it here down but you should be able to see the point 下面是一个如何实现该目标的示例,请记住我没有测试它,因为我在这里写下了它,但是您应该能够看到要点

$arrValidationData = array();
$arrValidationRules = array();

$arrCatchValidationData = array(
    "itemCode" => array(
        "label" => "Item Code",
        "rules" => "required"
    ),
    "Desc" => array(
        "label" => "Description",
        "rules" => "required"
    ),
    "Qty" => array(
        "label" => "Quantity",
        "rules" => "required|numeric"
    ),
    "Cost" => array(
        "label" => "Cost",
        "rules" => "required|numeric"
    ),
);

if (!empty($JSON)) {
    foreach ($JSON AS $k => $data) {

        foreach ($data AS $key => $value) {

            $keyToCatch = str_replace($k, "", $key);
            if (isset($arrCatchValidationData[$keyToCatch]))
            {
                $arrValidationData[$key] = $value;
                $arrValidationRules[] = array(
                    "field" => $key,
                    "label" => $arrCatchValidationData[$keyToCatch]['label'],
                    "required" => $arrCatchValidationData[$keyToCatch]['rules']
                );
            }
        }
        //$this->form_validation->reset_validation();

    }

    $this->form_validation->set_data($arrValidationData);
    $this->form_validation->set_rules($arrValidationRules);

}

update: 30.05.2016 更新:30.05.2016

according to your comment you want to validate post and json data in the same call, in this case you simply have to merge the data 根据您的评论,您想在同一调用中验证post和json数据,在这种情况下,您只需要合并数据

$arrValidationData = array_merge($arrValidationData, $_POST);

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

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