Question

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.

Was it helpful?

Solution

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!

OTHER TIPS

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

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'));

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.

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

label[for=payment_cardNumber]
{
    margin-top: 20px;
    color: red;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top