Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change label text of the ActiveField?

I have created new Yii2 basic project and want to dig in.

There is a Username field on login page: enter image description here

I want to change label 'Username' to a custom one, e.g. 'My superb label'. I have read the manual: http://www.yiiframework.com/doc-2.0/yii-widgets-activefield.html

After investigating a little I've got the next result: enter image description here

I have changed only template and it has changed the layout:

<?= $form->field($model, 'username', [
    "template" => "<label> My superb label </label>\n{input}\n{hint}\n{error}"
])?>

How to change the text of the label in a correct way? What is best practice?

like image 255
Vadim Avatar asked Nov 30 '14 23:11

Vadim


3 Answers

<?= $form->field($model, 'username')->textInput()->label('My superb label') ?>

http://www.yiiframework.com/doc-2.0/yii-bootstrap-activefield.html#label()-detail

like image 82
Mihai P. Avatar answered Nov 03 '22 15:11

Mihai P.


there is an another cool way.

<?= $form->field($model, 'username')->textInput(['class'=>'field-class'])->label('Your Label',['class'=>'label-class']) ?>
like image 22
Tayyab Hussain Avatar answered Nov 03 '22 14:11

Tayyab Hussain


Okay, just override attributeLabels in LoginForm.php:

/**
 * Returns the attribute labels.
 *
 * See Model class for more details
 *  
 * @return array attribute labels (name => label).
 */
public function attributeLabels()
{
    return [
        'username' => 'Логин',
        'password' => 'Пароль',
    ];
}
like image 19
Vadim Avatar answered Nov 03 '22 14:11

Vadim