codeigniter 3关联数组的验证

时间:2016-05-25 09:26:17

标签: php arrays codeigniter validation

我正在尝试使用验证库验证从JavaScript推送的关联数组。

以下代码正在工作,除了它只是为关联数组中的最后一个数组进行验证(查找值),是否有一种方法可以使它在foreach中运行时为每个数组实例工作?

代码:

    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();

        }
    }

数组输出:

[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
)

1 个答案:

答案 0 :(得分:1)

问题是,set_data函数在set_rules函数和CI之间被调用

  

在定义任何验证规则之前,您必须调用 set_data()方法。

有关详细信息,请查看documentation

一种可能的方法是捕获数组中的所有数据和规则

下面是一个如何实现这一点的例子,请记住我没有测试过,因为我在这里写下来但你应该能够看到这一点

$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);

}

更新:30.05.2016

根据你的评论你要在同一个调用中验证post和json数据,在这种情况下你只需要合并数据

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