2014-10-09 38 views
0

我有在特定文件夾中的元素這樣Symfony2中形成與選擇不OPTGROUP

$finder = new Finder(); 
$all_images = $finder->files()->in('images/questions'); 
$images = array(); 
foreach ($all_images as $image) { 
    $images[$image->getRelativePathName()] = $image->getRelativePathName(); 
} 

所述的var_dump輸出的陣列是:

array (size=3) 
     'image3.png' => string 'image3.png' (length=10) 
     'image2.png' => string 'image2.png' (length=10) 
     'image1.png' => string 'image1.png' (length=10) 

和創建在「選擇」元素我的形式是這樣的:

->add('imageA', 'choice', array('choices' => array($images) 

我顯示這個表單元素在這樣的樹枝(的CSS不事項):

{{ form_widget(form.imageA, { 'attr': {'class': 'rounded' } }) }} 

此生成此HTML代碼:

<select id="create_question_type_imageA" name="create_question_type[imageA]" class="rounded"> 
    <option value=""></option> 
    <optgroup label="0"> 
     <option value="image3.png">image3.png</option> 
     <option value="image2.png">image2.png</option> 
     <option value="image1.png">image1.png</option> 
    </optgroup> 
</select> 

我需要這個選擇沒有OPTGROUP,我儘量擴大爲false,但沒有工作...

TY!

回答

5

由於您的$ images變量已經是一個數組,您不需要在表單類型中再次指定它爲數組。否則你的$圖像將是一個數組數組,這就是爲什麼Symfony添加了optgroup。

相反的:

->add('imageA', 'choice', array('choices' => array($images)

嘗試只需設置:

->add('imageA', 'choice', array('choices' => $images)

+0

OMG,我花2小時在這...非常感謝! – 2014-10-09 08:08:35