![](/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.