php yii2 ActiveForm 字段添加html代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23311242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:38:12  来源:igfitidea点击:

yii2 ActiveForm field add html code

phpfieldyii2

提问by user3575904

I have this code in Yii2:

我在 Yii2 中有这个代码:

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

generate this:

生成这个:

<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>

and i want to do this

我想这样做

<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>

It's possible with the original source?

原始来源可以吗?

回答by user1852788

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

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

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

your code should be like this:

你的代码应该是这样的:

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