如何在表单输入字段中添加类?

问题描述 投票:0回答:2

我正在尝试将jscolor添加到输入字段,但它不能与我合作!

在配置功能中:

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

我也尝试在BaseForm中添加以下内容:

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

错误:sfWidgetFormInput函数中不允许类!

我想添加一个类jscolor!如何通过JavaScript或使用此配置添加它?

javascript php symfony1 symfony-forms symfony-1.2
2个回答
1
投票

在渲染模板中的表单元素时,需要将类添加到输入中:

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

2
投票

sfWidgetFormInput(实际上,任何symfony1小部件)都有2个参数 - $options$attributes。您将该类作为选项而不是作为属性传递。在模板中调用render()时添加class属性是一个选项,但我可能会选择(在大多数情况下)在设置表单时,在configure方法中执行此操作

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

baseForm可能是自动生成的,我强烈建议不要修改它。

© www.soinside.com 2019 - 2024. All rights reserved.