添加 CSS 属性到 Symfony 表单标签?
我尝试向自定义 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->getCSS())。
有人可以照亮我吗?
非常感谢。
I try to add some css attributes to labels in my custom sfForm but I can't achieve it.
In my custom class myForm extends sfForm
, I create all textfields dynamically:
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);
}
I tried to format the rendering with setFormFormatterName
but without success.
Note: I can't use renderLabel($value, $attributes = array())
in the template because I get the CSS class from the DB (as you may have seen, I have to use: $fixture->getCSS()).
Could someone shed my light?
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是我解决问题的方法。
我采纳了 johnwards 和 richsage 的建议,并将它们放在一起:
“这类事情应该在视图/操作中处理。”
“访问传递给 Widget 本身的选项/属性。”
首先,我将 CSS 类添加到输入本身(即使我不会使用它)。
在我的自定义类
myForm extends sfForm
中,然后在模板中,我不使用
echo $form;
,而是将 CSS 类添加到标签中,如下所示:也许可以不是解决此问题的最佳方法,但它有效。
感谢大家的反馈!
Here is how I solved it.
I took both suggestions from johnwards and richsage and put them together :
"This sort of stuff should be handled in the view/action."
"Access to the options/attributes passed to the Widget itself."
First, I add the CSS class to the input itself (even if I will not use it).
In my custom class
myForm extends sfForm
,Then, in the template, instead of using
echo $form;
, I add the CSS class to the label as shown below:Maybe it is not the best way to solve this issue but it works.
Thanks all for your feedback!
你想要做的事情似乎是可能的,但你的灰色语法似乎有点不对劲。试试这个:
...或在这里检查“sfWidgetForm基类”部分:http://www.symfony-project.org/forms/1_4/en/A-Widgets
What you're trying to do seems possible but your greyed out syntax seems a little off. Try this:
... or check "The sfWidgetForm Base Class" section here: http://www.symfony-project.org/forms/1_4/en/A-Widgets
对于所有表单小部件,类属性需要作为第二个参数中的数组传入。
The class attribute needs to be passed in as an array in the second parameter for all form widgets.
如果您想将 CSS 添加到标签(特别是呈现标签的方法),您可以尝试覆盖小部件类。然后,您可以执行类似的操作
,然后重写您的小部件中的
renderLabel()
方法或类似方法。您的小部件将可以访问您传入的选项 - 在上面的示例中,myCSSClass
是选项键。然后,您可以将此类值应用到小部件的标签。You could try overriding the widget class if you want to add CSS to labels - specifically the methods that render the label. You could then do something like
and then override your
renderLabel()
method or similar in your widget. Your widget will have access to the options you pass in - in the above examplemyCSSClass
is the option key. You can then apply this class value to your widget's label.这比那容易得多。只需将 CSS 应用于标签的标签...
It's much easier than that. Just apply CSS to the label for tag...