CAKEPHP重新填充国家自动化表格 - >输入选择编辑

时间:2012-11-27 03:40:10

标签: forms cakephp cakephp-2.0 helper formhelper

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

在控制器中:

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,我可以在Session中看到保存的双字母国家ISO代码:     billing_country CA

..对于shipping_country ...

但是当我回到页面时,该值将返回“美国”(选择中的第一个值...

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

1 个答案:

答案 0 :(得分: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']
));  
?>