Attribute labels are mainly used for display purpose. For example, given an attribute
firstName, we can declare a label First Name which is more user-friendly and can
be displayed to end users.
By default an attribute label is generated using Model::generateAttributeLabel.
This method allows you to explicitly specify attribute labels.
Note, in order to inherit labels defined in the parent class, a child class needs to
merge the parent labels with child labels using functions such as array_merge().
public attributeLabels ( ) : array | ||
리턴 | array | attribute labels (name => label) |
/** * @inheritdoc */ public function attributeLabels() { $labels = parent::attributeLabels(); $labels['name'] = Yii::t('app', 'Name'); $labels['email'] = Yii::t('app', 'Email'); $labels['subject'] = Yii::t('app', 'Subject'); $labels['body'] = Yii::t('app', 'Body'); $labels['verifyCode'] = Yii::t('app', 'Verification Code'); return $labels; }
/** * @inheritDoc */ public function attributeLabels() { return ArrayHelper::merge(parent::attributeLabels(), ['url' => 'URL']); }
public function attributeLabels() { return array_merge(parent::attributeLabels(), ['name' => \Yii::t('app', 'Name'), 'version' => \Yii::t('app', 'Installed version'), 'alias' => \Yii::t('app', 'Aliases')]); }
/** * @inheritDoc */ public function attributeLabels() { return ArrayHelper::merge(parent::attributeLabels(), ['attribute' => 'Атрибут']); }
public function testDefaults() { $singer = new Model(); $this->assertEquals([], $singer->rules()); $this->assertEquals([], $singer->attributeLabels()); }
public function attributeLabels() { return \yii\helpers\ArrayHelper::merge(parent::attributeLabels(), ['username' => 'email']); }
public function attributeLabels() { $labels = ['username' => '用户名', 'email' => '邮箱', 'password' => '密码', 'confirmPassword' => '确认密码', 'verifyCode' => '验证码']; return array_merge(parent::attributeLabels(), $labels); }
/** * @return array customized attribute labels */ public function attributeLabels() { return array_merge(parent::attributeLabels(), ['verifyCode' => Yii::t('app', 'Verification Code')]); }
public function attributeLabels() { $labels = ['username' => '用户名', 'password' => '密码', 'rememberMe' => '记住我']; return array_merge(parent::attributeLabels(), $labels); }