php - 如何防止 CakePHP 表单下拉列表忽略与 optgroup 匹配的选项?

标签 php cakephp

我的 View 中有一个带有下拉选项的表单字段。

echo $this->Form->input('form.selection', array('label' => 'Selection', 'type' => 'select', 'options' => $selection, 'empty' => '[None]));

表单字段从 $selection 变量获取其选项列表,该变量是一个嵌套数组,以便帮助使长选项列表更易于阅读。

$selection = array(
    'Section 1' => array(
        '1' => 'Section 1',
        '2' => 'Testing 1',
        '3' => 'Testing 2',
        '4' => 'Testing 3'
    ),
    'Section 2' => array(
        '5' => 'Testing 4',
        '6' => 'Testing 5',
        '7' => 'Testing 6',
        '8' => 'Testing 7'
    ),
);

我发现一个问题,如果 <option> 之一文本与 <optgroup> 的名称匹配然后它将其从列表中排除,这是我不希望发生的情况。

是否有合适的方法可以解决此问题,而无需手动创建 <select>元素和里面的所有选项?

注释: 为了这个问题的目的,我替换了数据数组,因为它包含我不想共享的潜在敏感信息,并且只是完整数组的一个选择。

开发环境: CakePHP 2.8

最佳答案

我刚刚查了一下 source FormHelper 并发现以下内容。

/* While a nested options array will create optgroups with options inside them.
 * ```
 * $options = array(
 *  1 => 'bill',
 *  'fred' => array(
 *     2 => 'fred',
 *     3 => 'fred jr.'
 *  )
 * );
 * $this->Form->select('Model.field', $options);
 * ```
 *
 * In the above `2 => 'fred'` will not generate an option element. You should enable the `showParents`
 * attribute to show the fred option.

因此,您需要在选择选项中将 showParents 设置为 true。看这里:https://api.cakephp.org/2.8/class-FormHelper.html#_select

关于php - 如何防止 CakePHP 表单下拉列表忽略与 optgroup 匹配的选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43250249/

相关文章:

php - 将 CodeIgniter (PHP) 与 Backbone Js 混合使用

php - 重写完整的项目有用吗?了解建立主页的想法

php - PDO 的 Mysql_data_seek 吗?

php - AJAX 调用成功后更新元素

php - cakephp:seo 友好的 url 路由 - 避免传递 url 参数

mysql - 在 CakePHP ORM 中使用计数子查询

cakephp - 如何通过在 CakePHP 3 中组合/拥有两个字段来创建键值对(显示字段)?

php - Oauth 认证 facebook - cakePHP 插件

php - 无法使用 PHP 变量列名更新 SQL 中的列

cakephp - 如何在cakephp 1.2中集成smarty?