简体   繁体   中英

Not able to set more than one value option for select object Zend Framework 2

I'm trying to fill a select object in Zend Framework 2 with an array of options. Basically, I am trying to grab all the directories and set them as both the option value and label, but it is only writing one directory as an option value when there is more than one directory that exists. Here is my code:

$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)
        )
    )); 
}

When I view the option, it lists the last directory and not all of them. I think it is overwriting the values stored but I have no idea on how to not let this happen.

Here is a screenshot of my issue if it helps: http://imgur.com/RbfGoIo

The list of directories screenshot: http://imgur.com/bZQgNcW

Like I said earlier it is only showing the directory test album_2017_07_14 in the select dropdown menu.

Any help would be appreciated.

Thanks!

You should move setValueOptions out of the 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); 

The issue is that setValueOptions sets the values of your select and does not append new values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM