Yii框架中的form表单

韦小宝
Lepaskan: 2023-03-17 17:16:01
asal
2802 orang telah melayarinya

使用过yii框架的同学都知道,yii框架中的form表单可以使用yii内部定义form组件来进行提交,小编今天就带着大家来看看,yii中的form表单组件吧!

话不多说上代码:

<?php
//引入命名空间
use yii\helpers\Html;
?>

<?php //表单:Html::beginForm(提交地址,提交方法,属性数组);?>

$form = ActiveForm::begin([
    &#39;action&#39; => [&#39;test/getpost&#39;],
    &#39;method&#39;=>&#39;post&#39;,
    ]); ?>

<?=Html::beginForm(&#39;&#39;,&#39;post&#39;,[&#39;id&#39;=>&#39;form&#39;,&#39;class&#39;=>&#39;form&#39;,&#39;data&#39;=>&#39;myself&#39;]);?>

<?php //(二)输入框:Html::input(类型,name值,默认值,属性数组;)?>

<?=Html::input(&#39;text&#39;,&#39;test&#39;,&#39;&#39;,[&#39;class&#39;=>&#39;form-control&#39;,&#39;placeholder&#39;=>&#39;hehe&#39;])->hint(&#39;Please enter your test&#39;)->label(&#39;Name&#39;);?>
<?=Html::input(&#39;email&#39;,&#39;email&#39;,&#39;admin@admin.com&#39;,[&#39;class&#39;=>&#39;form-control&#39;]);?>

<?=Html::input(&#39;password&#39;,&#39;pwd&#39;,&#39;&#39;,[&#39;class&#39;=>&#39;form-control&#39;]);?>
<?Html::input(&#39;hidden&#39;,&#39;hidden&#39;,&#39;&#39;,[&#39;class&#39;=>&#39;form-control&#39;]);?>

<hr/>

<?php //Html::表单类型input(name值,默认值,属性数值);?>

<?=Html::textInput(&#39;test&#39;,&#39;hehe&#39;,[&#39;class&#39;=>&#39;form-control&#39;]);?>
<?=Html::textInput(&#39;email&#39;,&#39;admin@admin.com&#39;,[&#39;class&#39;=>&#39;form-control&#39;]);?>

<?Html::passwordInput(&#39;pwd&#39;,&#39;&#39;,[&#39;class&#39;=>&#39;form-control&#39;]);?>
<?Html::hiddenInput(&#39;hidden&#39;,&#39;&#39;,[&#39;class&#39;=>&#39;form-control&#39;]);?>
<hr/>
<?php //(三) 文本域 Html::textarea()?>
<?=Html::textarea(&#39;area&#39;,&#39;&#39;,[&#39;class&#39;=>&#39;form-control&#39;,&#39;row&#39;=>&#39;3&#39;]);?>

<hr/>

<?php //单选按钮 Html::checkbox(name值,是否选中,属性数组)?>
<?=Html::radio(&#39;sex&#39;,true,[&#39;class&#39;=>&#39;form-control&#39;]);?>

<?=Html::radioList(&#39;height&#39;,&#39;1&#39;,[&#39;1&#39;=>&#39;160&#39;,&#39;2&#39;=>&#39;170&#39;,&#39;3&#39;=>&#39;180&#39;],[&#39;class&#39;=>&#39;form-control&#39;]);?>
<?php //复选框?>
<?=Html::checkbox(&#39;haha&#39;,true,[&#39;class&#39;=>&#39;form-control&#39;]);?>
<?php //复选框列表?>
<?=Html::checkboxList(&#39;xixi&#39;,&#39;1&#39;,[&#39;1&#39;=>&#39;160&#39;,&#39;2&#39;=>&#39;170&#39;,&#39;3&#39;=>&#39;180&#39;],[&#39;class&#39;=>&#39;form-control&#39;]);?>

<?php //下拉列表?>
<?=Html::dropDownList(&#39;list&#39;,&#39;2&#39;,[&#39;1&#39;=>&#39;160&#39;,&#39;2&#39;=>&#39;170&#39;,&#39;3&#39;=>&#39;180&#39;],[&#39;class&#39;=>&#39;form-control&#39;])?>

<?=Html::label(&#39;显示的&#39;,&#39;test&#39;,[&#39;style&#39;=>&#39;color:#ff0000&#39;]);?>
<hr/>
<?php //上传控件?>
<?=Html::fileInput(&#39;img&#39;,null,[&#39;class&#39;=>&#39;btn btn-default&#39;]);?>
<hr/>
<?php //按钮?>
<?=Html::button(&#39;普通按钮&#39;,[&#39;class&#39;=>&#39;btn btn-primary&#39;]);?>

<?=Html::submitButton(&#39;提交按钮&#39;,[&#39;class&#39;=>&#39;btn btn-primary&#39;]);?>

<?=Html::resetButton(&#39;重置按钮&#39;,[&#39;class&#39;=>&#39;btn btn-primary&#39;]);?>

<?=Html::endForm()?>
Salin selepas log masuk


文本框:textInput();
密码框:passwordInput();
单选框:radio(),radioList();
复选框:checkbox(),checkboxList();
下拉框:dropDownList();
隐藏域:hiddenInput();
文本域:textarea([‘rows’=>3]);
文件上传:fileInput();
提交按钮:submitButton();
重置按钮:resetButtun();

以下是代码示例:

<?php
$form = ActiveForm::begin([&#39;action&#39; => [&#39;test/getpost&#39;],&#39;method&#39;=>&#39;post&#39;,]); ?>

<? echo $form->field($model, &#39;username&#39;)->textInput([&#39;maxlength&#39; => 20]) ?>

<? echo $form->field($model, &#39;password&#39;)->passwordInput([&#39;maxlength&#39; => 20]) ?>

<? echo $form->field($model, &#39;sex&#39;)->radioList([&#39;1&#39;=>&#39;男&#39;,&#39;0&#39;=>&#39;女&#39;]) ?>

<? echo $form->field($model, &#39;edu&#39;)->dropDownList([&#39;1&#39;=>&#39;大学&#39;,&#39;2&#39;=>&#39;高中&#39;,&#39;3&#39;=>&#39;初中&#39;], [&#39;prompt&#39;=>&#39;请选择&#39;,&#39;style&#39;=>&#39;width:120px&#39;]) ?>

<? echo $form->field($model, &#39;file&#39;)->fileInput() ?>

<? echo $form->field($model, &#39;hobby&#39;)->checkboxList([&#39;0&#39;=>&#39;篮球&#39;,&#39;1&#39;=>&#39;足球&#39;,&#39;2&#39;=>&#39;羽毛球&#39;,&#39;3&#39;=>&#39;乒乓球&#39;]) ?>

<? echo $form->field($model, &#39;info&#39;)->textarea([&#39;rows&#39;=>3]) ?>

<? echo $form->field($model, &#39;userid&#39;)->hiddenInput([&#39;value&#39;=>3]) ?>

<? echo Html::submitButton(&#39;提交&#39;, [&#39;class&#39;=>&#39;btn btn-primary&#39;,&#39;name&#39; =>&#39;submit-button&#39;]) ?>

<? echo Html::resetButton(&#39;重置&#39;, [&#39;class&#39;=>&#39;btn btn-primary&#39;,&#39;name&#39; =>&#39;submit-button&#39;]) ?>

<?php ActiveForm::end(); ?>
Salin selepas log masuk

以上就是本章所有内容,希望会给大家带来帮助。

相关推荐:

加载Yii自带的验证码功能的方法

Yii2实现增删改查后留在当前页的方法详解

Yii表单模型使用及以数组形式提交表单数据_PHP教程

Atas ialah kandungan terperinci Yii框架中的form表单. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan