【问题标题】:Not able to set more than one value option for select object Zend Framework 2无法为选择对象 Zend Framework 2 设置多个值选项
【发布时间】:2017-07-16 21:45:58
【问题描述】:

我正在尝试用一组选项填充 Zend Framework 2 中的选择对象。基本上,我试图抓取所有目录并将它们设置为选项值和标签,但是当存在多个目录时,它只会将一个目录作为选项值写入。这是我的代码:

$form = new AddPhotosForm();


foreach (glob(getcwd() . '/public/images/profile/' . $identity . '/albums/*', GLOB_ONLYDIR) as $dir) {
    $form->get('copy-from-album')->setValueOptions(array(
        array(
            'value' => basename($dir), 'label' => basename($dir)
        )
    )); 
}

当我查看该选项时,它会列出最后一个目录,而不是所有目录。我认为它正在覆盖存储的值,但我不知道如何不让这种情况发生。

如果有帮助,这是我的问题的屏幕截图:http://imgur.com/RbfGoIo

目录列表截图:http://imgur.com/bZQgNcW

就像我之前说的,它只在选择下拉菜单中显示目录 test album_2017_07_14。

任何帮助将不胜感激。

谢谢!

【问题讨论】:

    标签: php zend-framework


    【解决方案1】:

    您应该将 setValueOptions 移出 foreach。

    $options = array();
    foreach (glob(getcwd() . '/public/images/profile/' . $identity . '/albums/*', GLOB_ONLYDIR) as $dir) {
        $options[] = array(
                'value' => basename($dir), 'label' => basename($dir)
            ); 
    }
    $form->get('copy-from-album')->setValueOptions($options); 
    

    问题在于 setValueOptions 设置您选择的值并且不附加新值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-26
      • 1970-01-01
      • 2017-02-05
      • 1970-01-01
      相关资源
      最近更新 更多