【问题标题】:$this->request->is('post') returns FALSE on form submit$this->request->is('post') 在表单提交时返回 FALSE
【发布时间】:2012-08-06 16:41:17
【问题描述】:

模型

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>

我的问题是提交表单的时候,controller返回false for request->is('false');

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

form 方法在没有设置的情况下就已经发布了,这有点令人沮丧。

我是不是做错了什么?

【问题讨论】:

  • 尝试使用print_r 显示$this-&gt;request 的值。我遇到了类似的问题,出于某种奇怪的原因,使用的提交方法是 PUT 而不是 POST(不过你的观点似乎很好)。

标签: php cakephp request


【解决方案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);
        }

    }

【讨论】:

  • 是的,Abid,我刚刚看到 Cake 使用表单助手生成的隐藏字段来设置/识别方法。而在添加表单中,request->data 为空,隐藏字段名为“_method”的值为“POST”,在编辑表单中,request->data 是由 read() 方法创建的数组型号,值为“PUT”。谢谢你的回答!
  • 我认为它也可以在条件中使用以下内容:(!empty($this->request->data))。它更短。
【解决方案2】:

我不确定您使用的是什么框架,但我的猜测是在视图中 $this->Form->create()

应该有一个选项供您将表单操作类型设置为发布。

如果您查看 PHP 代码生成表单的 html,是否会创建 ?我的猜测是它可能默认执行 GET。

【讨论】:

    猜你喜欢
    • 2011-06-22
    • 1970-01-01
    • 2014-12-07
    • 2013-10-20
    • 1970-01-01
    • 2016-08-25
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多