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 回答