user2636556
user2636556

Reputation: 1915

change placeholder text from array

I'm using the yii-user extension and i'm trying the add proper label to the 'placeholder' attribute. really new to Yii so still trying to get the grasp of things.

I've added the attributeLabels() method in the class in the models folder.

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

Here is my code in my /views/ folder

       $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); 

            }
        }
?>

how would i make attributeLabels() work on my echo $form->textField($profile,$field->varname,array('size'=>60,'maxlength'=>(($field->field_size)?$field->field_size:255),'class' => 'input-block-level', 'placeholder' => $placeholder)); ?

Upvotes: 0

Views: 4604

Answers (2)

srakl
srakl

Reputation: 2619

you don't have to edit class RegistrationForm extends User

open protected/modules/user/model/User.php

add add/edit your custom labels in the attributeLabels() method

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"),
    );
}

and to get the label to show in your view file. use this

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

Upvotes: 1

secretlm
secretlm

Reputation: 2361

You can get the text label for the specified attribute with getAttributeLabel() like:

$model->getAttributeLabel('verifyPassword');

E.x:

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

Upvotes: 2

Related Questions