简体   繁体   English

CakePHP:“GET”表单在提交后不会自动填充表单字段

[英]CakePHP: “GET” form does not auto-populate form fields after submission

I'm using Cake 2.3.0. 我正在使用Cake 2.3.0。 If I submit my form using POST , the selected form fields carry over however if I submit my form using GET , all of the form fields return to their default values. 如果我使用POST提交表单,则所选表单字段会结束,但如果我使用GET提交表单,则所有表单字段都将返回其默认值。

Is there a way to make the GET submission to work like that of the POST ? 有没有办法让GET提交像POST一样工作?

Here's my contorller: 这是我的控制器:

class ListingsController extends AppController {
    public function results() {
        $conditions = array(
            'Listing.Beds >=' => $this->request->query['beds'], 
            'Listing.ListingStatus >=' => $this->request->query['status'], 
        );

        $this->paginate = array(
            'conditions' => $conditions,
        );    

        $this->set('listings', $this->paginate());
    }
}

Here's what my view looks like. 这是我的观点。

echo $this->Form->create(null, array(
    'controller' => 'listings', 
    'action' => 'results',
    'type' => 'get'
));

echo $this->Form->input('name');

$beds = array('1' => '1+', '2' => '2+', '3' => '3+', '4' => '4+', '5' => '5+');
echo $this->Form->input('beds', array('options' => $beds));

$status = array('Active' => 'Active', 'Pending' => 'Pending', 'ActivePending' => 'Active and Pending');
echo $this->Form->input('status', array('options' => $status));

echo $this->Form->end('Update'); 

So basically if I change 'type' => 'get' to 'type' => 'post' it works just fine. 所以基本上如果我将'type' => 'get'改为'type' => 'post'它就可以了。 But I need to be able to do this via GET . 但我需要能够通过GET来做到这一点。

Thanks 谢谢

I agree that this is annoying (IMO CakePHP should be smart enough to automatically determin 'where' to get its data from, based on the 'type'). 我同意这很烦人(IMO CakePHP应该足够聪明,根据'类型'自动确定“从哪里获取数据”。

You'll have to copy the 'query' of the request to the 'data' of the request; 您必须将请求的“查询”复制到请求的“数据”;

$this->request->data = $this->request->query;

Not behind my computer to test it (as usual, lol), but should probably work. 不是我的电脑后面测试它(像往常一样,哈哈),但应该可行。

尝试将此添加到您的控制器:

$this->request->data = $this->params['url'];

My solution: 我的解决方案

$this->params->data = array('Tablefilter' => $this->params->query);

where "Tablefilter" depends on form definition (usually the model name) 其中“Tablefilter”取决于表单定义(通常是模型名称)

$this->Form->create('Tablefilter', array('type' => 'get'))

Use PRG . 使用PRG Checkout this plugin. 看看这个插件。

What I ended up doing was looping through the $this->request->query array and pass those values to the matching $this->request->data . 我最终做的是循环遍历$this->request->query数组并将这些值传递给匹配的$this->request->data

So I added this: 所以我补充说:

foreach($this->request->query as $k => $v){
    $this->request->data['Listing'][$k] = $this->request->query[$k];
}

Which ultimately gave me this: 这最终给了我这个:

class ListingsController extends AppController {
    public function results() {

        foreach($this->request->query as $k => $v){
            $this->request->data['Listing'][$k] = $this->request->query[$k];
        }

        $conditions = array(
            'Listing.Beds >=' => $this->request->query['beds'], 
            'Listing.ListingStatus >=' => $this->request->query['status'], 
        );

        $this->paginate = array(
            'conditions' => $conditions,
        );    

        $this->set('listings', $this->paginate());
    }
}

I'm not going to accept this as the answer though as I don't know if this is the optimal or suggested way to do it. 我不会接受这个作为答案,因为我不知道这是否是最佳或建议的方式。 But it works for me for now. 但它现在对我有用。

This works for me : 这对我有用:

$this->request->data['Cluster'] = $this->params->query ; $ this-> request-> data ['Cluster'] = $ this-> params-> query; //controller side //控制器端

Form definition: 表格定义:

$this->Form->create('Cluster',array('type'=>'get')); $这 - >形式 - >创建( '群集',阵列( '类型'=> 'GET'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM