1

モデル

class CompanyCategory extends AppModel
{
    public $name = "CompanyCategory";

    public $hasMany = array(
        "Company"
    );
}

コントローラー

public function admin_edit($id = null){

            //debug($this->request);
            //exit(0);
            if($id == null){
                $this->Session->setFlash("ID categorie eronat!", "flash/simpla_error");
                $this->redirect("index");
            }
            if($this->request->is('post')){
                if($this->CompanyCategory->save($this->request->data)){
                    $this->Session->setFlash("Categoria a fost salvata!", "flash/simpla_success");
                }
                else{
                    $this->Session->setFlash("Categoria NU a fost salvata!", "flash/simpla_error");
                }
            }
            else{
                $this->Session->setFlash("READ!", "flash/simpla_error");
                $this->request->data = $this->CompanyCategory->read(null, $id);
            }
        }

景色

<div class="content-box">
    <div class="content-box-header">
        <h3>Editeaza categorie firme</h3>
    </div>
    <div class="content-box-content">
        <?php
            echo $this->Form->create("CompanyCategory", array(
                'inputDefaults' => array(
                    'error' => array(
                        'attributes' => array(
                            'wrap' => 'span', 
                            'class' => 'input-notification error png_bg'
                        )                   
                    )
                )
            ));
        ?>

        <?=$this->Form->input('id', array('type' => 'hidden'))?>

        <?=$this->Form->input('title', array('class' => "text-input small-input", 'label' => 'Denumire'))?>

        <?=$this->Form->submit('Salveaza', array('class' => "button"))?>
    </div>
</div>

私の問題は、フォームを送信するときに、コントローラーが request->is('false'); に対して false を返すことです。

createメソッド内のフォームヘルパーのビューで明示的にタイプを「post」として設定すると、期待どおりに機能します。

フォームメソッドを設定せずに投稿しているのに少しイライラします。

私は何か間違っていますか?

4

2 に答える 2

1

このコントローラーを使う

public function admin_edit($id = null) {
        $this->layout = 'admin_layout';
        $this->CompanyCategory->id = $id;
        if (!$this->CompanyCategory->exists()) {
            throw new NotFoundException(__('Invalid CompanyCategory model'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->CompanyCategory->save($this->request->data)) {
                $this->Session->setFlash(__('The CompanyCategory model has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The CompanyCategory model could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->CompanyCategory->read(null, $id);
        }

    }
于 2012-08-06T16:58:06.733 に答える
-1

これに使用しているフレームワークはわかりませんが、$this->Form->create() のビューにあると思います

フォーム アクション タイプを投稿に設定するオプションがあるはずです。

PHP コードがフォームを生成している html を表示すると、? のアクション プロパティが作成されます。私の推測では、おそらくデフォルトで GET を実行しています。

于 2012-08-06T16:51:07.333 に答える