yii2:active 表单生成代码
yii2:active form generate code
我在 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>
抱歉我的英语不好
你需要使用这样的东西
<div class="form-group">
<?= Html::activeTextInput($model, 'username', ['class' => 'form-control']) ?>
<?= Html::activeTextInput($model, 'password', ['class' => 'form-control']) ?>
</div>
使用 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']) ?>
我在 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>
抱歉我的英语不好
你需要使用这样的东西
<div class="form-group">
<?= Html::activeTextInput($model, 'username', ['class' => 'form-control']) ?>
<?= Html::activeTextInput($model, 'password', ['class' => 'form-control']) ?>
</div>
使用 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']) ?>