Cake PHP将get参数添加到表单中



我想有条件地将GET参数添加到CakePHP中的表单操作中,但默认的操作行为似乎覆盖了我希望将其设置为的内容:

我尝试了这个,结果$formaction是我想要的表单操作,除了:

$formaction = '/edit/'.$this->data['Shipment']['id'];
$formaction = isset($trace_param)? '?trace_action='.$trace_action.'&trace_param='.$trace_param : '';
echo $this->Form->create('Shipment', array('action'=> $formaction ));

这导致动作为shipments/shipments/edit/7101?trace_action=scheduled_shipments&trace_param=2013-03-18/7101

所以我尝试将模型设置为null。。但它总是将装运id附加到表单操作的末尾。我也尝试过用html硬编码<form>标签,但这导致数据不在提交的表单中。当我把它放回原始的echo $this->Form->create('Shipment');时,它又起作用了。

有没有一种可靠的方法可以将get参数附加到Cake中的表单中?(该站点使用1.3.7版本)

操作!=url

如果设置了action密钥,则控制器操作,即:

/controller_name/<this bit>/other/args

要明确设置表单将提交到的url,请使用url键:

echo $this->Form->create('Shipment', array('url'=> $formaction));

不要将URL作为字符串进行操作

Cake中的Url通常被定义为数组,它们更灵活,更容易使用。问题中的url可以写成:

$formaction = array(
    'action' => 'edit',
    $this->data['Shipment']['id']
);
if ($trace_param) {
    $formaction['?'] = array(
        'trace_action' => $trace_action
        'trace_param' => $trace_param
    )
}
echo $this->Form->create('Shipment', array('url'=> $formaction));

或者只使用隐藏的表单输入

这通常让生活变得非常简单:

echo $this->Form->create('Shipment');
if ($trace_param) {
    echo $this->Form->hidden('trace_action', array('value' => $trace_action));
    echo $this->Form->hidden('trace_param', array('value' => $trace_param));
}

最新更新