我已按照本指南http://www.yiiframework.com/doc-2.0/guide-input-tabular-input.html建立表格输入。但在我的情况下,我想为每个输入添加一个不同的标签。我怎么能这样做?
更新行动:
public function actionUpdate()
{
$emailModel = EMAIL::find()->indexBy('ID')->all();
if (Model::loadMultiple($emailModel, Yii::$app->request->post()) && Model::validateMultiple($emailModel)) {
foreach ($emailModel as $email) {
$email->save(false);
}
return $this->redirect('update');
}
return $this->render('update', ['emailModel' => $emailModel]);
}
更新视图
<?php $form = ActiveForm::begin(); ?>
<?php foreach ($emailModel as $index => $email) { ?>
<?= $form->field($email, "[$index]CONTENT")->textArea(['maxlength' => true])->label(false) ?>
<?php } ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
我正在学习Yii2。谢谢。
答案 0 :(得分:2)
更改多个模型的标签与一个模型相同。
1)从attributeLabels()
检索它或根据相应的数据库列名自动生成(如果它是ActiveRecord
并且没有相应的条目attributeLabels()
)只需省略->label(false)
来电:
<?= $form->field($email, "[$index]CONTENT")->textArea(['maxlength' => true]) ?>
2)仅为此表单应用自定义标签:
<?= $form->field($email, "[$index]CONTENT")->textArea(['maxlength' => true])->label('Your custom label') ?>
3)要为集合中的每个$model
设置不同的标签,只需创建辅助函数并在循环中调用它:
function getCustomLabel($model, $index)
{
$number = $index + 1;
return "Content for model number $number";
}
<?= $form->field($email, "[$index]CONTENT")->textArea(['maxlength' => true])->label(getCustomLabel($model, $index)) ?>
查看官方文档:
答案 1 :(得分:0)
label(label name)
<?php $form = ActiveForm::begin(); ?>
<?php foreach ($emailModel as $index => $email) { ?>
<?= $form->field($email, "[$index]CONTENT")->textArea(['maxlength' => true])->label('Email '.$index+1) ?>
<?php } ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
参阅文档 Yii2 Active Field