我的 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/