好吧,經過大量的研究,我已經管理了某種解決方案,但也許有更正確的東西?我不使用sfWidgetFormDoctrineChoice,而是使用sfWidgetFormSelectRadio(但複選框也可以,但我不知道它是否可以與其他小部件一起工作,或者甚至選擇小部件:/僅僅因爲我的業務規則需要它,在這種特殊情況下SelectRadio已經足夠了......)
小部件的選項選項填充了前面查詢的結果,我用它來填充先前處理的DoctrineChoice小部件,以便每個記錄的ID是每個選擇的關鍵和價值:
$imgs = Doctrine_Core::getTable('ProjImages')->getImages();
$choices = array('' => '');
foreach ($imgs as $img):
$choices[$img->getId()] = $img->getId();
endforeach;
接下來,我還通過了'formatte R」選項,窗口小部件:
$this->widgetSchema['img'] = new sfWidgetFormSelectRadio(array(
'choices' => $choices,
'formatter' => array($this, 'showAsImages')
));
$this->validatorSchema['img'] = new sfValidatorChoice(array(
'choices' => $choices,
'required' => false
));
我用了‘需要’=>假以驗證選擇,因爲我還需要的選項,選擇‘在我的小部件,它體現在$選擇沒有圖像’數組作爲第一個(''=>'')選擇。
最後,我寫格式化回調:
public function showAsImages($widget, $inputs)
{
$rows = array();
foreach ($inputs as $input)
{
$domdoc = new DOMDocument();
$domdoc->loadHTML($input['label']);
$node = $domdoc->getElementsByTagName('label')->item(0);
if ($node->nodeValue != "")
{
$img = Doctrine_Core::getTable('ProjImages')->find(array($node->nodeValue));
$input['label'] = '<label '.$node->attributes->item(0)->name .
'="'.$node->attributes->item(0)->value.'">' .
'<img src="'.$img->getImg().'" alt="image" />' .
'</label>';
}
$rows[] = $widget->renderContentTag('li',
$input['input'].
$widget->getOption('label_separator').
$input['label']);
}
return $widget->renderContentTag('ul',
implode($widget->getOption('separator'), $rows),
array('class' => $widget->getOption('class')));
}
我使用的源代碼的sfWidgetFormSelectRadio的原始默認格式並基於它,我修改了每個輸入元素的「標籤」(所有的其餘代碼與我使用的源代碼完全相同)。
對於每個輸入元素的標籤,我使用DOMDocument對象來獲取值(圖像的id),然後進行數據庫查詢以獲取圖像,然後重新組裝「標籤」 < img>標籤...當然,如果我碰巧找到空的選擇,我使用默認的'標籤'...
就是這樣......我認爲格式化回調可以得到更多的工作,所以我們歡迎任何建議,甚至更好的解決方案,正如您所看到的,我依賴於窗口小部件的「格式化程序」選項,而據我所知,只有一些小部件接受此選項。 ..
感謝您的閱讀!