添加 CSS 属性到 Symfony 表单标签?

发布于 2024-09-07 09:27:50 字数 855 浏览 3 评论 0原文

我尝试向自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

木槿暧夏七纪年 2024-09-14 09:27:51

这是我解决问题的方法。
我采纳了 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())  
         );  
}  

然后在模板中,我不使用 echo $form;,而是将 CSS 类添加到标签中,如下所示:

foreach ($form as $widgetId => $widget) {  
    ...
    $labelClass = $widget->getWidget()->getAttribute('class');  
    echo '<td>'.$widget->renderLabel(null, array('class' => $labelClass)).'</td>';  
    ...
}

也许可以不是解决此问题的最佳方法,但它有效。

感谢大家的反馈!

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,

foreach ($fixtures as $fixture) {    
     $widgetFixtures[$fixture->getId()] = new sfWidgetFormInputText(  
         array('label' => $fixture->getTeamNameDom()),  
         array('value' => $fixture->getScore1(), 
               'readonly' => 'readonly',  
               'class' => $fixture->getCSS())  
         );  
}  

Then, in the template, instead of using echo $form;, I add the CSS class to the label as shown below:

foreach ($form as $widgetId => $widget) {  
    ...
    $labelClass = $widget->getWidget()->getAttribute('class');  
    echo '<td>'.$widget->renderLabel(null, array('class' => $labelClass)).'</td>';  
    ...
}

Maybe it is not the best way to solve this issue but it works.

Thanks all for your feedback!

薄凉少年不暖心 2024-09-14 09:27:51

你想要做的事情似乎是可能的,但你的灰色语法似乎有点不对劲。试试这个:

    $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

What you're trying to do seems possible but your greyed out syntax seems a little off. Try this:

    $widgetFixtures[$fixture->getId()] = new sfWidgetFormInputText(  
        array('label' => $fixture->getTeamNameDom()),  
        array('class' => $fixture->getCSS()),
        array('value' => $fixture->getScore1(), 'readonly' => 'readonly')
    );

... or check "The sfWidgetForm Base Class" section here: http://www.symfony-project.org/forms/1_4/en/A-Widgets

诺曦 2024-09-14 09:27:51

对于所有表单小部件,类属性需要作为第二个参数中的数组传入。

$this->setWidget('foo', new sfWidgetFormInputText(array(),array('class','fooclass'));

The class attribute needs to be passed in as an array in the second parameter for all form widgets.

$this->setWidget('foo', new sfWidgetFormInputText(array(),array('class','fooclass'));
柠栀 2024-09-14 09:27:51

如果您想将 CSS 添加到标签(特别是呈现标签的方法),您可以尝试覆盖小部件类。然后,您可以执行类似的操作

$foo = new myWidgetFormInputText(array("myCSSClass" => $fixture->getCSS()));

,然后重写您的小部件中的 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

$foo = new myWidgetFormInputText(array("myCSSClass" => $fixture->getCSS()));

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 example myCSSClass is the option key. You can then apply this class value to your widget's label.

何以畏孤独 2024-09-14 09:27:51

这比那容易得多。只需将 CSS 应用于标签的标签...

label[for=payment_cardNumber]
{
    margin-top: 20px;
    color: red;
}

It's much easier than that. Just apply CSS to the label for tag...

label[for=payment_cardNumber]
{
    margin-top: 20px;
    color: red;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文