我尝试在自定义sfForm中为标签添加一些css属性,但我无法实现。
在我的自定义类myForm extends sfForm
中,我动态创建所有文本字段:
public function configure()
{
$widgetFixtures = array();
foreach ($fixtures as $fixture) {
$widgetFixtures[$fixture->getId()] = new sfWidgetFormInputText(
array('label' => $fixture->getTeamNameDom()),
// I would like to add something like: array('class' => $fixture->getCSS()),
array('value' => $fixture->getScore1(), 'readonly' => 'readonly')
);
}
$this->setWidgets($widgetFixtures);
}
我尝试使用setFormFormatterName
格式化渲染,但没有成功。
注意:我不能在模板中使用renderLabel($value, $attributes = array())
,因为我从数据库中获取了CSS类(如您所见,我必须使用:$ fixture-&gt; getCSS())。< / p>
有人可以解释我的光吗?
非常感谢。
答案 0 :(得分:1)
以下是我如何解决它
我从johnwards和richsage那里得到了两个建议并将它们放在一起:
“这种东西应该在视图/动作中处理。”
“访问传递给Widget本身的选项/属性。”
首先,我将CSS类添加到输入本身(即使我不会使用它)。
在我的自定义课程myForm extends sfForm
中,
foreach ($fixtures as $fixture) {
$widgetFixtures[$fixture->getId()] = new sfWidgetFormInputText(
array('label' => $fixture->getTeamNameDom()),
array('value' => $fixture->getScore1(),
'readonly' => 'readonly',
'class' => $fixture->getCSS())
);
}
然后,在模板中,我将CSS类添加到标签中,而不是使用echo $form;
,如下所示:
foreach ($form as $widgetId => $widget) {
...
$labelClass = $widget->getWidget()->getAttribute('class');
echo '<td>'.$widget->renderLabel(null, array('class' => $labelClass)).'</td>';
...
}
也许这不是解决这个问题的最佳方法,但它确实有效。
感谢大家的反馈!
答案 1 :(得分:0)
你想要做的事情似乎有可能但是你的灰色语法似乎有些偏差。试试这个:
$widgetFixtures[$fixture->getId()] = new sfWidgetFormInputText(
array('label' => $fixture->getTeamNameDom()),
array('class' => $fixture->getCSS()),
array('value' => $fixture->getScore1(), 'readonly' => 'readonly')
);
...或者在此处查看“sfWidgetForm基类”部分:http://www.symfony-project.org/forms/1_4/en/A-Widgets
答案 2 :(得分:0)
class属性需要作为数组传入所有表单小部件的第二个参数。
$this->setWidget('foo', new sfWidgetFormInputText(array(),array('class','fooclass'));
答案 3 :(得分:0)
如果要将CSS添加到标签(特别是呈现标签的方法),可以尝试覆盖窗口小部件类。然后你可以做类似
的事情$foo = new myWidgetFormInputText(array("myCSSClass" => $fixture->getCSS()));
然后覆盖您的小部件中的renderLabel()
方法或类似方法。您的小部件可以访问您传入的选项 - 在上面的示例中,myCSSClass
是选项密钥。然后,您可以将此类值应用于窗口小部件的标签。
答案 4 :(得分:0)
比这容易得多。只需将CSS应用于标签的标签......
label[for=payment_cardNumber]
{
margin-top: 20px;
color: red;
}