2012-11-27 79 views
0

我正在建立一個購物車,堅持在用戶編輯地址時重新填充國家選擇輸入。CAKEPHP重新填充國家automagic表格 - >輸入選擇編輯

在控制器:

public function loadCountryList() { 

    $this->loadModel('GeoCountry'); 
    $geoCountryList = $this->GeoCountry->find('all', array(
     'recursive' => -1, 
     'order' => array('GeoCountry.name' => 'ASC') 
    )); 

    $geoCountries = array('Select Country' => array('US' => 'United States', 'CA' => 'Canada')); 
    while(list($key,$row) = each($geoCountryList)) { 
     $geoCountries['International Countries'][$row['GeoCountry']['id']] = $row['GeoCountry']['name']; 
    } 
    $this->set('geoCountries', $geoCountries); 

} 

結帳/編輯地址屏幕上選擇正確呈現與結構化的控制器和值正確張貼,並如預期保存在會話optgroups一個選擇。

<?php echo $this->Form->input('geoCountries', array('class' => 'span3', 'label' => 'Billing Country', 'name' => 'data[Order][billing_country]', 'id' => 'OrderBillingCountry')); ?> 

和輸出:

<select name="data[Order][billing_country]" class="span3" id="OrderBillingCountry"> 
<optgroup label="Select Country"> 
<option value="US">United States</option> 
<option value="CA">Canada</option> 
</optgroup> 
<optgroup label="International Countries"> 
<option value="AF">Afghanistan</option> 
…. 
</optgroup> 
</select> 

使用DebugKit,我可以看到保存在會話的兩個字母的國家ISO代碼: billing_country CA

..同爲shipping_country ...

但是,當我回到頁面時,該值返回到「美國」(第一個值在選擇...

那麼我錯過了什麼?我一直在撕掉我的頭髮!

回答

1

我想你缺少這部分控制器:

$this->request->data['Order']['billing_country'] = ....; 

以選擇的值傳遞給視圖....

編輯: 鑑於你也必須添加默認值像這樣:

<?php 
echo $this->Form->input('geoCountries', array(
    'class' => 'span3', 
    'label' => 'Billing Country', 
    'name' => 'data[Order][billing_country]', 
    'id' => 'OrderBillingCountry', 
    'type' => 'select', 
    'default' => $this->data['Order']['billing_country'] 
)); 
?> 
+0

不,這不是我想的。在控制器中,訂單被傳遞給$ this-> request,如下所示:'if(!empty($ shop ['Order'])){ \t \t $ this-> request-> data [訂單'] = $ shop ['Order']; \t \t}'如果我調試,我可以看到shipping_country和billing_country ISO代碼,這是下拉值的關鍵。 – tremendusapps

+0

我想我得到了錯誤;)... se更新回答 –

+0

100%正確! ...你是個好人,謝謝。當以前選擇一個值時需要設置「默認」的邏輯有點奇怪,但它的工作原理:)謝謝。 – tremendusapps