Dev Troubleshooter
Dev Troubleshooter

Reputation: 115

How to add class to form input field?

I am trying to add jscolor to an input field but it is not working with me!

In configure function:

public function configure() {
        unset($this['created_at'],$this['clicks']);
        $this->widgetSchema['background_color']->sfWidgetFormInput('class'=> 'jscolor');
} 

I also tried adding the below in BaseForm:

$this->setWidgets(array(
            'id'            => new sfWidgetFormInputHidden(),
            'background_color'  => new sfWidgetFormInput(array('class'=> 'jscolor')),
            'url_link'      => new sfWidgetFormInput(),
            'status'        => new sfWidgetFormInput(),
        ));

Error: Class is not allowed in the sfWidgetFormInput function !

I want to add a class jscolor! How can i add it via JavaScript or using this configuration?

Upvotes: 0

Views: 271

Answers (2)

Michael Kopinsky
Michael Kopinsky

Reputation: 904

An sfWidgetFormInput (really, any symfony1 widget) takes 2 parameters - $options, and $attributes. You are passing the class as an option rather than as an attribute. Adding the class attribute when you call render() in the template is one option, but I would probably opt (in most cases) to do it when setting up the form, in the configure method

public function configure() {
        unset($this['created_at'], $this['clicks']);

        $this->setWidget('background_color', new sfWidgetFormInput(array(), array('class'=> 'jscolor'));

        // or to be a bit more minimalistic:
        $this->getWidget('background_color')->setAttribute('class','jscolor');
}

The baseForm is probably autogenerated, and I would strongly recommend against modifying it.

Upvotes: 2

Umair Shah
Umair Shah

Reputation: 2280

You need to add your class to your input during rendering the form element in your template as :

<?php echo $form['background_color']->render(array('class' => 'jscolor')); ?>

Upvotes: 1

Related Questions