1

I have a simple question, I know how to change an the color of an field in a magento form but my problem is i didn't know how to change the color of the label associate to it.

here is an image :

color field

and here is my code :

$fieldset->addField('modeleChoix', 'select', array(

        'label' => 'Modele selection(only update)',

        'name' => 'modeleChoix',

        'style' => 'color:red',

        'values' => $tableau2,

        'onchange' => 'majChampModele()',

    ));
1

2 Answers 2

1

Unfortunately, Magento does not provide an out of the box method to change the style of the label.

See the getLabelHtml method from Varien_Data_Form_Element_Abstract :

public function getLabelHtml($idSuffix = '')
{
    if (!is_null($this->getLabel())) {
        $html = '<label for="'.$this->getHtmlId() . $idSuffix . '">' . $this->_escape($this->getLabel())
              . ( $this->getRequired() ? ' <span class="required">*</span>' : '' ) . '</label>' . "\n";
    } else {
        $html = '';
    }
    return $html;
}

I guess the only way you can do it is by creating your own renderer that extends Varien_Data_Form_Element_Select and rewrites that methods to add the style attribute to the label element.

An alternative would be to use JavaScript to change the style on the fly but that's kind of hacky.

2

Their are two solution for this.

1) if you inspect any admin form their will be a <div> under a <form> which contains a id which is i think generated based on tab name and section name which you have used in your form creation process. for ex faq_tabs_form_section_content

By using this id you can create a css rule for a label. which will be like

#faq_tabs_form_section_content label {
    color: #CB1D1D !important;
}

create this rule in any existing css or create a new css. i used the existing one skin\adminhtml\default\default\reset.css

Note: this will change a color of all labels of that form.

2) If you want to change a color for a particular label only.

When you create a field for a form by using a code some thing like

$fieldset->addField(
            "customer_name", "text", array(
            "label" => Mage::helper("test_faq")->__("Customer Name"),
            "name" => "customer_name",
            'required' => true,
            )
        );

Add 'container_id' => 'customeCustomerName', into this declaration will create a id of that <tr>. On which basis you can create a css rule.

so your declaration becomes like.

$fieldset->addField(
            "customer_name", "text", array(
            "label" => Mage::helper("test_faq")->__("Customer Name"),
            "name" => "customer_name",
            'required' => true,
            'container_id' => 'customeCustomerName',
            )
        );

and the css rule will be

#customeCustomerName label{
color: #440088 !important;
font-style: italic;
font-weight: bold;

}

and your result will be like.

enter image description here

Hope this helps you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.