【问题标题】:CAKEPHP re-populate country automagic Form->Input select on editCAKEPHP 重新填充国家自动表单-> 编辑时输入选择
【发布时间】:2012-11-14 15:34:46
【问题描述】:

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

在控制器中:

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 代码: billing_country CA

.. shipping_country 相同 ...

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

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

【问题讨论】:

    标签: forms cakephp cakephp-2.0 helper formhelper


    【解决方案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']
    ));  
    ?>
    

    【讨论】:

    • 不,我认为不是这样。在控制器中,订单作为数组传递给 $this->request,如下所示:if(!empty($shop['Order'])) { $this-&gt;request-&gt;data['Order'] = $shop['Order']; },如果我调试它,我可以看到 shipping_country 和 billing_country ISO 代码,这是下拉值的关键。
    • 100% 正确! ……你是个好人,谢谢。在之前选择了一个值时需要设置一个“默认值”的逻辑有点奇怪,但它可以工作:) 谢谢。
    • PS - 这是因为它是“自定义”选择输入还是平台正常 - 我在食谱中没有看到对此的参考。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多