How to change label text of the ActiveField?
Asked Answered
T

5

32

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?

Telepathy answered 30/11, 2014 at 23:24 Comment(0)
T
19

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' => 'Пароль',
    ];
}
Telepathy answered 20/12, 2014 at 21:45 Comment(0)
C
55
<?= $form->field($model, 'username')->textInput()->label('My superb label') ?>

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

Cluster answered 30/11, 2014 at 23:27 Comment(1)
Personally I'd consider this the correct answer, because the question does not state that label should be changed in all instances. For example, consider a model where one wishes to alter the label in _form, because a drop-down is being used for a related table value, whilst the remote ID is displayed in other scenarios.Bang
L
24

there is an another cool way.

<?= $form->field($model, 'username')->textInput(['class'=>'field-class'])->label('Your Label',['class'=>'label-class']) ?>
Leibniz answered 23/7, 2015 at 6:2 Comment(2)
really helped thisConfection
You are a rock star! attributeLabels was not an option here for me as I had multiple route/templates on single model.Case
T
19

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' => 'Пароль',
    ];
}
Telepathy answered 20/12, 2014 at 21:45 Comment(0)
C
2

You can also add such function to model:

public function attributeLabels()
{
    return [
        'username' => 'My Login',
        'password' => 'My Pasword',
        'rememberMe' => 'Remember Me, please',
    ];
}
Contaminate answered 22/9, 2016 at 8:45 Comment(0)
A
0

just change the lable from modles like this

'symptomsBefore' => Yii::t('app', 'Has The Patient Suffered from the same or similar symptoms before'),
Anagnos answered 3/7, 2020 at 10:35 Comment(1)
This could easily be a comment try to give meaningful answer with an explanation to support your answer. Have a look at thisNichol

© 2022 - 2024 — McMap. All rights reserved.