tec*_*hic 3 symfony1 symfony-1.4 symfony-forms
我正在安装Symfony 1.4,我们需要重新设计以匹配新的公司品牌.新皮肤的要求之一是单选按钮具有以下语法 -
<label><input name="foo" type="radio" value="bar" id="foo_bar"><span></span></label>
Run Code Online (Sandbox Code Playgroud)
但是单选按钮的默认Symfony代码是 -
<input name="foo" type="radio" value="bar" id="foo_bar">
Run Code Online (Sandbox Code Playgroud)
我可以更改模板在哪里添加附加元素以包围输入标记?我通过代码搜索了高低,我似乎无法找到生成此代码的位置.
提前致谢!
您需要创建自定义窗口小部件类.我扩展了无线电小部件并更改了格式化程序功能.
您可能需要自己定制一点.但是下面的myWidgetFormSelectRadio类会将每个无线电字段包装在您指定的标签中.
// \lib\widget\myWidgetFormSelectRadio.class.php
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[] = array(
'input' => $this->renderContentTag('label', $this->renderTag('input', array_merge($baseAttributes, $attributes)), array('for' => $id)),
);
}
return call_user_func($this->getOption('formatter'), $this, $inputs);
}
public function formatter($widget, $inputs)
{
$rows = array();
foreach ($inputs as $input)
{
$rows[] = $input['input'].$this->getOption('label_separator');
}
return implode($this->getOption('separator'), $rows);
}
}
Run Code Online (Sandbox Code Playgroud)
然后在你的表单类中
// \lib\form\myForm.class.php
public function configure()
{
$this->widgetSchema['my_field'] = new myWidgetFormSelectRadio(array('choices' => $choices));
// ....
}
Run Code Online (Sandbox Code Playgroud)
您是偶然使用Twitter Bootstrap吗?如果是这样,那么您还需要扩展Symfony复选框小部件.如果你这样做,那么你可能想要扩展sfWidgetFormChoice类并更改getRenderer()方法,以便它在渲染radio/checkbox字段时使用你的myWidgetFormSelectRadio和myWidgetFormSelectCheckbox类.
// \lib\widget\myWidgetFormChoice.php
class myWidgetFormChoice extends sfWidgetFormChoice
{
public function getRenderer()
{
if ($this->getOption('renderer'))
{
return $this->getOption('renderer');
}
$prefix = $this->getOption('use_my_custom_widget') ? 'my' : 'sf';
if (!$class = $this->getOption('renderer_class'))
{
$type = !$this->getOption('expanded') ? '' : ($this->getOption('multiple') ? 'checkbox' : 'radio');
$class = sprintf($prefix . 'WidgetFormSelect%s', ucfirst($type));
}
return new $class(array_merge(array('choices' => new sfCallable(array($this, 'getChoices'))), $this->options['renderer_options']), $this->getAttributes());
}
}
Run Code Online (Sandbox Code Playgroud)
然后在你的表单类中
public function configure()
{
$this->widgetSchema['my_field'] = new myWidgetFormChoice(array(
'choices' => $choices,
'expanded' => true,
'use_my_custom_widget' => true,
));
// ....
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3132 次 |
| 最近记录: |