![](/img/trans.png)
[英]CakePHP does not auto-populate a field (that has no Model) after submission
[英]CakePHP: “GET” form does not auto-populate form fields after submission
我正在使用Cake 2.3.0。 如果我使用POST
提交表单,则所选表单字段会结束,但如果我使用GET
提交表单,则所有表单字段都将返回其默认值。
有没有办法让GET
提交像POST
一样工作?
这是我的控制器:
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());
}
}
这是我的观点。
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');
所以基本上如果我将'type' => 'get'
改为'type' => 'post'
它就可以了。 但我需要能够通过GET
来做到这一点。
谢谢
我同意这很烦人(IMO CakePHP应该足够聪明,根据'类型'自动确定“从哪里获取数据”。
您必须将请求的“查询”复制到请求的“数据”;
$this->request->data = $this->request->query;
不是我的电脑后面测试它(像往常一样,哈哈),但应该可行。
尝试将此添加到您的控制器:
$this->request->data = $this->params['url'];
我的解决方案
$this->params->data = array('Tablefilter' => $this->params->query);
其中“Tablefilter”取决于表单定义(通常是模型名称)
$this->Form->create('Tablefilter', array('type' => 'get'))
我最终做的是循环遍历$this->request->query
数组并将这些值传递给匹配的$this->request->data
。
所以我补充说:
foreach($this->request->query as $k => $v){
$this->request->data['Listing'][$k] = $this->request->query[$k];
}
这最终给了我这个:
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());
}
}
我不会接受这个作为答案,因为我不知道这是否是最佳或建议的方式。 但它现在对我有用。
这对我有用:
$ this-> request-> data ['Cluster'] = $ this-> params-> query; //控制器端
表格定义:
$这 - >形式 - >创建( '群集',阵列( '类型'=> 'GET'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.