new sfWidgetFormSelectRadio(
array('choices' => $images)));
以上将呈现每个选项,例如:
<input type="radio" name="name" value="image_path">
如何使用最少的代码使其呈现这种方式:
<input type="radio" name="name" value="image_path"><img src="image_path" />
这是未经测试的,直接来自我阅读该小部件的 Symfony API 文档。您需要扩展sfWidgetFormSelectRadio该类,将其命名为类似myWidgetFormSelectRadio并将其粘贴lib/widget/myWidgetFormSelectRadio.class.php到您的项目中。
像这样覆盖formatChoices()方法:
class myWidgetFormSelectRadio extends sfWidgetFormSelectRadio
{
protected function formatChoices($name, $value, $choices, $attributes)
{
$inputs = array();
foreach ($choices as $key => $option)
{
$baseAttributes = array(
'name' => substr($name, 0, -2),
'type' => 'radio',
'value' => self::escapeOnce($key),
'id' => $id = $this->generateId($name, self::escapeOnce($key)),
);
if (strval($key) == strval($value === false ? 0 : $value))
{
$baseAttributes['checked'] = 'checked';
}
$inputs[$id] = array(
'input' =>
$this->renderTag('input', array_merge($baseAttributes, $attributes))
. $this->renderTag('img', array('src' => self::escapeOnce($key))),
'label' => $this->renderContentTag('label', self::escapeOnce($option), array('for' => $id)),
);
}
return call_user_func($this->getOption('formatter'), $this, $inputs);
}
}
所以你基本上是将img标签附加到输入中。
然后,在表单的configure()方法中,您需要从使用切换sfWidgetFormSelectRadio到使用myWidgetFormSelectRadio以使用新的小部件。
让我知道这个是否奏效 ;-)