0
public $form = array (  
    array(  
        'field' => 'email',  
        'params' => array(  
            array(  
                'rule' => 'email',  
                'on' => 'create',  
                'required' => true,  
                'error' => 'The email is invalid!'  
            ),  
            array(  
                'rule' => 'email',  
                'on' => 'update',  
                'required' => false,  
                'error' => 'The email is invalid!'  
            )  
        )  
    )  
);


public function onlyNeeded($action) {
    $form = $this->form;
    $action = $this->action;

    foreach ($form as $formelement) {
        $field = $formelement['field'];
        $paramsgroup = $formelement['params'];
        if ($paramsgroup['on'] != $action) {
            form = removeparamsgroup($form, $action);
            }
        }
    return $form;
}

我该如何做这个removeparamsgroup()功能?

有 [index] ,而不仅仅是 [name]!

你懂我的意思吗?

数组(数组(两次!

4

3 回答 3

1

如果您在 foreach 循环中获得数组键,则可以使用它取消设置正确的数组索引。您还需要遍历每个表单元素的每个参数,您在示例中没有这样做。

public function onlyNeeded($action) {
    $form = $this->form;

    //get $formelement by reference so it can be modified
    foreach ($form as & $formelement) {

        //$key becomes the index of current $param in $formelement['params']
        foreach ($formelement['params'] as $key => $param) {
           if ($param['on'] != $action) {
               unset($formelement['params'][$key]);
           }
        }
    }
    return $form;
}
于 2009-05-14T14:56:21.180 回答
0

尝试这个。

function onlyNeeded($action) {
    $form = $this->form;

    foreach ($form as &$formelement) {
        foreach ($formelement['params'] as $key => $paramsgroup)
        {
            if ($paramsgroup['on'] != $action)
                unset($formelement['params'][$key]);
        }
    }
    return $form;
}

不要忘记第一个 foreach 循环中的 & 符号,否则它将不起作用。如果没有 & 符号,foreach 会复制每个元素而不是返回引用。

于 2009-05-14T15:01:11.497 回答
0

未设置($form['params'])?你说的删除是什么意思?

于 2009-05-14T14:36:40.087 回答