从数组中更改占位符文本

时间:2013-12-05 14:29:03

标签: yii

我正在使用yii-user扩展,我正在尝试为'placeholder'属性添加适当的标签。对Yii来说真的很新,所以仍然试图掌握一切。

我在模型文件夹中的类中添加了attributeLabels()方法。

class RegistrationForm extends User {
       /**
     * Declares attribute labels.
     */
    public function attributeLabels()
    {
        return array(
            'email'=>'Email Address',
            'firstname'=>'First Name',
            'lastname' => 'Last Name',
                    'verifyPassword' = 'Retype Password'
        );
    }
} 

以下是我的 / views / 文件夹

中的代码
       $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
            'id'=>'registration-form',
            'type'=>'vertical',
            'enableClientValidation'=>true,
            'clientOptions'=>array(
                'validateOnSubmit'=>true,
            ),
        ));


    <?php echo $form->textField($model,'email', array('class' => 'input-block-level', 'placeholder' => 'email')); ?>

            <?php echo $form->passwordField($model,'password', array('class' => 'input-block-level', 'placeholder' => 'password')); ?>

            <?php echo $form->passwordField($model,'verifyPassword', array('class' => 'input-block-level', 'placeholder' => 'verifyPassword')); ?>  


    <?php 
        $profileFields=Profile::getFields();
        if ($profileFields) {
            foreach($profileFields as $field) {

        if ($widgetEdit = $field->widgetEdit($profile)) {
            //echo $widgetEdit;
        } elseif ($field->range) {
            echo $form->dropDownList($profile,$field->varname,Profile::range($field->range),array('class' => 'input-block-level'));
        } elseif ($field->field_type=="TEXT") {
            echo $form->textArea($profile,$field->varname,array('rows'=>6, 'cols'=>50));
        } else {
            //echo $field->varname;
            if ($field->varname == 'firstname')
            {
                $placeholder = 'First Name';
            }
            else if ($field->varname == 'lastname')
            {
                $placeholder = 'Last Name';
            }
            else 
            {
                $placeholder = $field->varname; 
            }
echo $form->textField($profile,$field->varname,array('size'=>60,'maxlength'=>(($field->field_size)?$field->field_size:255),'class' => 'input-block-level', 'placeholder' => $placeholder));
        }

        echo $form->error($profile,$field->varname); 

            }
        }
?>

如何让我attributeLabels()上的echo $form->textField($profile,$field->varname,array('size'=>60,'maxlength'=>(($field->field_size)?$field->field_size:255),'class' => 'input-block-level', 'placeholder' => $placeholder));工作?

2 个答案:

答案 0 :(得分:2)

您可以使用getAttributeLabel()获取指定属性的文本标签,如:

$model->getAttributeLabel('verifyPassword');

E.x:

       <?php echo $form->passwordField($model,'verifyPassword',
              array('class' => 'input-block-level', 
             'placeholder' => $model->getAttributeLabel('verifyPassword')));
      ?>

答案 1 :(得分:1)

您无需修改​​class RegistrationForm extends User

打开 protected / modules / user / model / User.php

attributeLabels()方法

中添加/修改自定义标签
public function attributeLabels()
{
    return array(
        'id' => UserModule::t("Id"),
        'username'=>UserModule::t("username"),
        'password'=>UserModule::t("Password"),
        'verifyPassword'=>UserModule::t("Retype Password"),
        'firstname'=>UserModule::t("First Name"), //ADDED
        'lastname'=>UserModule::t("Last Name"), // ADDED
        'email'=>UserModule::t("Email Address"), //EDITED
        'verifyCode'=>UserModule::t("Verification Code"),
        'activkey' => UserModule::t("Activation Key"),
        'createtime' => UserModule::t("Registration Date"),
        'create_at' => UserModule::t("Registration Date"),
        'lastvisit_at' => UserModule::t("Last Visit"),
        'superuser' => UserModule::t("Superuser"),
        'status' => UserModule::t("Status"),
    );
}

并获取要在视图文件中显示的标签。使用这个

<?php echo $form->passwordField($model,'verifyPassword',
              array('class' => 'input-block-level', 
             'placeholder' => $model->getAttributeLabel('email')));
?>