簡體   English   中英

$ this-> request-> is('post')在表單提交時返回FALSE

[英]$this->request->is('post') returns FALSE on form submit

該模型

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”的類型將按預期工作。

表單方法已經發布但未設置它,這有點令人沮喪。

難道我做錯了什么 ?

使用這個控制器

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);
        }

    }

我不確定您使用的是哪個框架,但是我的猜測是在$ this-> Form-> create()的視圖中

您應該有一個選項將表單操作類型設置為post。

如果您查看在哪里有PHP代碼生成表單的html,是否正在創建的action屬性? 我的猜測是它可能默認情況下執行GET。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM