如何将类添加到Yii表单标签?

时间:2012-07-03 20:03:52

标签: forms yii label

要生成以下标记:

<label class="foo">Bar</label>

PHP看起来像:

<?php echo $form->label($model,'username'); ?>

好像它应该是label()的htmlOptions参数的一部分,但我无法弄明白。

来自Yii的文档:

public string label(CModel $model, string $attribute, array $htmlOptions=array ( ))

价:

http://www.yiiframework.com/doc/api/1.1/CActiveForm#label-detail

2 个答案:

答案 0 :(得分:9)

正如文档所说,$ htmlOptions是一组额外的HTML属性。键是属性,而值是属性值,因此要向标签添加类属性:

<?php echo $form->label( $model,'username', array('class'=>'className') ); ?>

答案 1 :(得分:0)

您还可以使用setLabelAttribute将类添加到PHP表单类中,例如:

<?php
class Login extends Yp_Form_Abstract
{

    public function init()
    {
...
        $username = new Yp_Form_Element(Yp_Form_Element::FIELD_textField, 'username', $this);
        $username->setLabel('Username');
        $username->setLabelAttribute('class', 'some-class');
    }
} 
相关问题