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

时间:2012-08-06 16:41:18

标签: php cakephp request

模特

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>

我的问题是,在提交表单时,控制器会为请求返回false-&gt; is('false');

如果我在视图中明确地设置了create方法中的表单助手,则将其类型设置为“post”,它按预期工作。

虽然表单方法已经发布而没有设置它,但有点令人沮丧。

我做错了吗?

2 个答案:

答案 0 :(得分: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);
        }

    }

答案 1 :(得分:-1)

我不确定你使用的框架是什么,但我的猜测是在视图中 $这 - &GT;形状配合&GT;创建()

您可以选择将表单操作类型设置为发布。

如果您查看PHP代码生成表单的html是否创建了?的action属性?我的猜测是它默认可能正在进行GET。