yii2 ActiveForm字段添加html代码

时间:2014-04-26 13:00:54

标签: php field yii2

我在Yii2中有这段代码:

<?= $form->field($model, 'username',$opzioni)->textInput(array('placeholder' => 'Username'));  ?>

生成这个:

<div class="input-icon field-loginform-username required">
    <label class="control-label" for="loginform-username">Username</label>
    <input id="loginform-username" class="form-control" type="text" placeholder="Username" name="LoginForm[username]">
    <div class="help-block"></div>
</div>

我希望这样做

<div class="input-icon field-loginform-username required">
    <label class="control-label" for="loginform-username">Username</label>
    **<i class="fa fa-user"></i>**
    <input id="loginform-username" class="form-control" type="text" placeholder="Username" name="LoginForm[username]">
    <div class="help-block"></div>
</div>

原始来源可能吗?

1 个答案:

答案 0 :(得分:41)

参考http://stuff.cebe.cc/yii2docs/yii-widgets-activefield.html# $ template-detail

template = "{label}\n{input}\n{hint}\n{error}"

你的代码应该是这样的:

<?= $form->field($model, 'username', [
  'template' => "{label}\n<i class='fa fa-user'></i>\n{input}\n{hint}\n{error}"
])->textInput(array('placeholder' => 'Username'));  ?>