0

私の見解では、送信ボタンとキャンセル ボタンのあるフォームがあります。どちらのアクションでも、インデックス ページに戻ります。唯一の違いは、Submit は通常のデータベース送信を行い、「Your Invoice has been updated.」というメッセージを表示するのに対し、Cancel は更新をキャンセルし、「更新がキャンセルされました.」と表示することです。コントローラーのコードは次のとおりです。

    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'));
?>   

ただし、どのボタンが押されても、常に最初のメッセージが返されます。また、db submit も実行します。

Netbeans で XDebug を使用しようとして失敗しましたが、それはまた別の話です。通常、私の間違いは他の人には明らかです。だから、誰かが私を軌道に乗せてくれることを願っています。

4

1 に答える 1

1

私はちょうど同じ問題に取り組んでいたところ、単にインデックスにリンクしなくても機能する解決策を見つけました。ワイリーのデバッグへの提案$this->paramsで、「フォーム」配列が作成されていないことに気付きました。ただし、'cancel' パラメータが定義されている 'data' という配列がありました。したがって、どのボタンが押されたかを確認しているコントローラーでは、代わりに

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

使用する

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

機能するキャンセルボタンが表示されます。

于 2012-11-02T15:21:13.913 に答える