yii 表单小部件

本文详细介绍了Yii2框架中各种表单元素的使用方法,包括文本框、密码框、单选框、复选框、下拉框、隐藏域、文本域、文件上传、提交按钮和重置按钮等。通过具体代码示例展示了如何创建表单和字段,以及如何设置属性。
摘要由CSDN通过智能技术生成
文本框<span class="hljs-selector-pseudo">:textInput()</span>;

密码框<span class="hljs-selector-pseudo">:passwordInput()</span>;

单选框<span class="hljs-selector-pseudo">:radio()</span>,<span class="hljs-selector-tag">radioList</span>();

复选框<span class="hljs-selector-pseudo">:checkbox()</span>,<span class="hljs-selector-tag">checkboxList</span>();

下拉框<span class="hljs-selector-pseudo">:dropDownList()</span>;

隐藏域<span class="hljs-selector-pseudo">:hiddenInput()</span>;

文本域<span class="hljs-selector-pseudo">:textarea(</span><span class="hljs-selector-attr">['rows'=>3]</span>);

文件上传<span class="hljs-selector-pseudo">:fileInput()</span>;

提交按钮<span class="hljs-selector-pseudo">:submitButton()</span>;

重置按钮<span class="hljs-selector-pseudo">:resetButtun()</span>;

&nbsp;

<span class="php"><span class="hljs-meta"><?php</span> $form = ActiveForm::begin([<span class="hljs-string">'action'</span> => [<span class="hljs-string">'test/getpost'</span>],<span class="hljs-string">'method'</span>=><span class="hljs-string">'post'</span>,]); <span class="hljs-meta">?></span></span>

<span class="php"><span class="hljs-meta"><?</span> <span class="hljs-keyword">echo</span> $form->field($model, <span class="hljs-string">'username'</span>)->textInput([<span class="hljs-string">'maxlength'</span> => <span class="hljs-number">20</span>]) <span class="hljs-meta">?></span></span>

<span class="php"><span class="hljs-meta"><?</span> <span class="hljs-keyword">echo</span> $form->field($model, <span class="hljs-string">'password'</span>)->passwordInput([<span class="hljs-string">'maxlength'</span> => <span class="hljs-number">20</span>]) <span class="hljs-meta">?></span></span>

<span class="php"><span class="hljs-meta"><?</span> <span class="hljs-keyword">echo</span> $form->field($model, <span class="hljs-string">'sex'</span>)->radioList([<span class="hljs-string">'1'</span>=><span class="hljs-string">'男'</span>,<span class="hljs-string">'0'</span>=><span class="hljs-string">'女'</span>]) <span class="hljs-meta">?></span></span>

<span class="php"><span class="hljs-meta"><?</span> <span class="hljs-keyword">echo</span> $form->field($model, <span class="hljs-string">'edu'</span>)->dropDownList([<span class="hljs-string">'1'</span>=><span class="hljs-string">'大学'</span>,<span class="hljs-string">'2'</span>=><span class="hljs-string">'高中'</span>,<span class="hljs-string">'3'</span>=><span class="hljs-string">'初中'</span>], [<span class="hljs-string">'prompt'</span>=><span class="hljs-string">'请选择'</span>,<span class="hljs-string">'style'</span>=><span class="hljs-string">'width:120px'</span>]) <span class="hljs-meta">?></span></span>

<span class="php"><span class="hljs-meta"><?</span> <span class="hljs-keyword">echo</span> $form->field($model, <span class="hljs-string">'file'</span>)->fileInput() <span class="hljs-meta">?></span></span>

<span class="php"><span class="hljs-meta"><?</span> <span class="hljs-keyword">echo</span> $form->field($model, <span class="hljs-string">'hobby'</span>)->checkboxList([<span class="hljs-string">'0'</span>=><span class="hljs-string">'篮球'</span>,<span class="hljs-string">'1'</span>=><span class="hljs-string">'足球'</span>,<span class="hljs-string">'2'</span>=><span class="hljs-string">'羽毛球'</span>,<span class="hljs-string">'3'</span>=><span class="hljs-string">'乒乓球'</span>]) <span class="hljs-meta">?></span></span>

<span class="php"><span class="hljs-meta"><?</span> <span class="hljs-keyword">echo</span> $form->field($model, <span class="hljs-string">'info'</span>)->textarea([<span class="hljs-string">'rows'</span>=><span class="hljs-number">3</span>]) <span class="hljs-meta">?></span></span>

<span class="php"><span class="hljs-meta"><?</span> <span class="hljs-keyword">echo</span> $form->field($model, <span class="hljs-string">'userid'</span>)->hiddenInput([<span class="hljs-string">'value'</span>=><span class="hljs-number">3</span>]) <span class="hljs-meta">?></span></span>

<span class="php"><span class="hljs-meta"><?</span> <span class="hljs-keyword">echo</span> Html::submitButton(<span class="hljs-string">'提交'</span>, [<span class="hljs-string">'class'</span>=><span class="hljs-string">'btn btn-primary'</span>,<span class="hljs-string">'name'</span> =><span class="hljs-string">'submit-button'</span>]) <span class="hljs-meta">?></span></span>

<span class="php"><span class="hljs-meta"><?</span> <span class="hljs-keyword">echo</span> Html::resetButton(<span class="hljs-string">'重置'</span>, [<span class="hljs-string">'class'</span>=><span class="hljs-string">'btn btn-primary'</span>,<span class="hljs-string">'name'</span> =><span class="hljs-string">'submit-button'</span>]) <span class="hljs-meta">?></span></span>

<span class="php"><span class="hljs-meta"><?php</span> ActiveForm::end(); <span class="hljs-meta">?></span></span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值