CAKEPHP 重新填充国家/地区自动魔术表单>输入选择编辑时



我正在构建一个购物车,在用户编辑地址时,我一直在重新填充国家/地区选择输入。

在控制器中:

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);
}

签出/编辑地址屏幕上的选择正确地呈现了具有控制器中结构的optgroup的选择,并且值按预期正确地发布和保存在会话中。

<?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,我可以看到Session中保存的两个字母的国家ISO代码:计费_国家CA

shipping_country也是如此。。。

但当我回到页面时,值返回到"美国"(选择…中的第一个值

那么我错过了什么??!我一直在扯我的头发!

我想你在控制器中缺少了这个部分:

$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']
));  
?>

最新更新