yii2:活动表单生成代码

时间:2015-03-24 09:49:51

标签: php html forms yii

我在Yii2中有这段代码:

<?= $form->field($model, 'username')->label(false); ?>
<?= $form->field($model, 'password')->label(false); ?>

它产生了这个:

<div class="form-group field-validate-username required has-success">
    <input type="text" id="validate-username" class="form-control" name="Validate[username]">
</div>
<div class="form-group field-validate-password required has-success">
    <input type="text" id="validate-password" class="form-control" name="Validate[password]">
</div>

但我想这样做:

<div class="form-group">
    <input type="text" name="MViewer[first_name]" class="form-control" id="mviewer-first_name">
    <input type="text" name="MViewer[last_name]" class="form-control" id="mviewer-last_name">
</div>

抱歉我的英文不好

2 个答案:

答案 0 :(得分:1)

你需要使用这样的东西

<div class="form-group"> <?= Html::activeTextInput($model, 'username', ['class' => 'form-control']) ?> <?= Html::activeTextInput($model, 'password', ['class' => 'form-control']) ?> </div>

答案 1 :(得分:0)

使用Html获得所需的输出。

使用yii \ bootstrap \ Html;

$firstName = $lastName = '';
if(!$model->isNewRecord){
$firstName = $model->first_name;
$lastName = $model->lastName;
}

<?= Html::textInput('MViewer[first_name]', $firstName, ['class'=>'form-control', 'id'=>'mviewer-first_name']) ?>

<?= Html::textInput('MViewer[last_name]', $lastName, ['class'=>'form-control', 'id'=>'mviewer-last_name']) ?>