CakePHP取消按钮不会触发

时间:2012-04-10 18:20:34

标签: forms controller cakephp-2.0

在我看来,我有一个提交和取消按钮的表单。这两个操作都将我返回到我的索引页面。唯一的区别是提交正常数据库提交并显示消息“您的发票已更新。”,而取消应取消更新并显示“更新已取消。”。这是控制器代码:

    public function edit($id = null) {
    $this->Invoice->id = $id;
    $this->set('invoice', $this->Invoice->read(null, $id));
    //Check for $_GET
    if ($this->request->is('get')) {
        $this->request->data = $this->Invoice->read();
    } else {
        // Bail if cancel button pressed
        if (isset($this->params['form']['cancel'])) {
            $this->Session->setFlash('Update canceled.');
            $this->redirect(array('action' => 'index'));
        } else if ($this->Invoice->save($this->request->data)) {
            $this->Session->setFlash('Your Invoice has been updated.');
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash('Unable to update your Invoice.');
        }
    }
}

以下是观点:

<fieldset>
<legend>Enter New Values</legend>
<li><?php echo $this->Form->input('purchaseOrderNumber'); ?></li>
<li><?php echo $this->Form->input('siteAddress'); ?></li>
<li><?php echo $this->Form->input('siteCity'); ?></li>
<li><?php echo $this->Form->input('siteState'); ?></li>
<?php
echo $this->Form->button('Submit Form', array('type' => 'submit'));
echo $this->Form->submit('Cancel', array('div' => false, 'name' => 'cancel'));
?>   

但是,无论按哪个按钮,它总是返回第一条消息。它还执行数据库提交。

我尝试使用XDebug与Netbeans失败,但这是一个不同时间的故事。通常我的错误对其他人来说很明显。所以,我希望有人能让我回到正轨。

1 个答案:

答案 0 :(得分:1)

我正在研究同样的问题,我找到了一个无需简单链接回索引的解决方案。在Wylie建议调试$this->params时,我注意到'form'数组没有被创建。但是,有一个名为'data'的数组,其中定义了'cancel'参数。 因此,在您正在检查按下哪个按钮的控制器中,而不是

if (isset($this->params['form']['cancel'])) {

使用

if (isset($this->params['data']['cancel'])) {

您将获得一个有效的取消按钮。

相关问题