2

是否可以禁用选择元素中的选项?

我有一个带有选择元素的表单,默认情况下有很多可用选项。在表单创建期间,根据从数据库中检索到的信息,我想禁用某些选项。

一些研究提出了 $form->get('selectElement')->setAttribute("disabled", array(0, 1, 2)); ......应该禁用前 3 个选项,但不幸的是没有。

4

1 回答 1

1

您必须使用该setAttribute()方法来设置select元素的属性,而不是其选项。为此,您应该使用setValueOptions()

$myOptions = $form->get('selectElement')->getValueOptions();
foreach ([0, 1, 2] as $value) {
    $myOptions [$value]['disabled'] = true ;
}
$form->get('selectElement')->setValueOptions($myOptions);

$myOptions必须是一个选项数组:

[
    [
        'label' => 'My first option',
        'disabled' => false,
        'value' => 1
    ],
    [
        'label' => '2nd option',
        'disabled' => false,
        'value' => 2
    ],
    [
        'label' => '3rd option disabled',
        'disabled' => true,
        'value' => 3
    ],
]
于 2016-08-16T02:56:40.447 回答