Disable some options select in Zend Framework 2

 ̄綄美尐妖づ 提交于 2019-12-11 03:33:30

问题


I would disable some option's select in Zend Framework 2. I have a select about spoken languages, when the user save a spoken language, I would disable it because he can't save again the same language.

inside LanguageForm.php

    $this->add(array(
            'name' => 'languages',
            'attributes' => array (
                    'class' => 'form-control',
            ),
            'type'  => 'select',
            'options' => array(
                    'label' => 'Languages',
                     'empty_option' => 'Select spoken languages',
                     'value_options' => array(
                                                1 => 'English', 
                                                2 => 'Spanish', 
                                                3 => 'German', 
                                                4 => 'Italian'
                                                .......... continue......
                                        ),
                             )));

inside my controller, I tried to do like this, but doesn't work. The function disables the entire select:

$spoken = array (1,2);  
$form->get('languages')->setAttribute('disabled', $spoken);

where am I wrong? thanks so much for the help.


回答1:


To disable some options you should provide not just a scalar label, but an array:

$options = $form->get('languages')->getValueOptions();
foreach ([1,2] as $value) 
{
     $options [$value] = [
        'label' => $options [$value],
        'disabled' => true,
        'value' => $value
    ];
}

$form->get('languages')->setValueOptions($options);


来源:https://stackoverflow.com/questions/31362010/disable-some-options-select-in-zend-framework-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!