views:

432

answers:

4

Hi,

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.

A: 

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

Tom
@Tom: sfWidgetFormInputText only accept 2 parameters $options=array() and $attributes=array(). I already had a look at symfony website and cannot found any info regarding putting css attributes to labels...Functions like "setLabel($name, $value)" doesn't solve the issue
Rev
A: 

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'));
johnwards
@johnwards: Tell me if I'm wrong but I think this syntax will add the CSS class to the input text. My issue is to assign CSS class to its label.
Rev
Ah no sorry, early morning my mistake. You are not going to be able to do that easily when customising the form. You are starting to get a bit too complicated for the form framework. This sort of stuff should be handled in the view/action.
johnwards
A: 

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.

richsage
Widgets don't have a renderLabel method, those are on the form fields. http://www.symfony-project.org/api/1_2/sfFormField Lots of really knarly stuff to get this to work where it would be simpler to do it in the action/view
johnwards
+1  A: 

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!

Rev