Activeform 文本框:textInput(); 密码框:passwordInput(); 单选框:radio(),radioList(); 复选框:checkbox(),checkboxList(); 下拉框:dropDownList(); 隐藏域:hiddenInput(); 文本域:textarea(['rows'=>3]); 文件上传:fileInput(); 提交按钮:submitButton(); 重置按钮:resetButtun();
<?php $form = ActiveForm::begin([ 'action' => ['test/getpost'], 'method'=>'post', ]); ?> <? echo $form->field($model, 'username')->textInput(['maxlength' => 20]) ?> <? echo $form->field($model, 'password')->passwordInput(['maxlength' => 20]) ?> <? echo $form->field($model, 'sex')->radioList(['1'=>'男','0'=>'女']) ?> <? echo $form->field($model, 'edu')->dropDownList(['1'=>'大学','2'=>'高中','3'=>'初中'], ['prompt'=>'请选择','style'=>'width:120px']) ?> <? echo $form->field($model, 'file')->fileInput() ?> <? echo $form->field($model, 'hobby')->checkboxList(['0'=>'篮球','1'=>'足球','2'=>'羽毛球','3'=>'乒乓球']) ?> <? echo $form->field($model, 'info')->textarea(['rows'=>3]) ?> <? echo $form->field($model, 'userid')->hiddenInput(['value'=>3]) ?> <? echo Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?> <? echo Html::resetButton('重置', ['class'=>'btn btn-primary','name' =>'submit-button']) ?> <?php ActiveForm::end(); ?>
使用方法
首先创建model层,因为要使用表单小部件 所以要加载相应的组件,这里需要的组件有yii\widgets\ActiveForm yii\helpers\Html
接下来在model定义的class里 写方法,首先我们要定义需要使用表单小部件的name值
<?php
/**
* Created by PhpStorm.
* User: jinlei
* Date: 2017/5/10
* Time: 9:35
*/
namespace
frontend\models;
use
yii\base\Model;
use
yii\widgets\ActiveForm;
use
yii\helpers\Html;
class
Form
extends
Model
{
public
$name
;
public
$pwd
;
public
$sex
;
public
$hobby
;
public
$age
;
public
function
rules(){
return
[
];
}
public
function
attributeLabels(){
return
[
‘name
'=>'
用户名',
‘pwd
'=>'
密码',
‘sex
'=>'
性别',
‘hobby
'=>'
爱好',
‘age
'=>'
年龄'
];
}
static
public
function
dataarr(
$data
){
$arr
=
array
();
foreach
(
$data
as
$key
=>
$value
){
$arr
[
$value
[‘kid
']] = $value[‘kname'
];
}
return
$arr
;
}
}
在这个model里 有将英文表头转换中文的方法 attributuLabels
还有 我们处理单选多选还有下拉框值得方式 dataarr
接下来 我 们需要创建controller
<?php
/**
* Created by PhpStorm.
* User: jinlei
* Date: 2017/5/10
* Time: 9:39
*/
namespace
frontend\controllers;
use
yii\web\Controller;
use
yii;
use
db;
use
frontend\models\Form;
class
LoginController
extends
Controller
{
public
function
actionIndex(){
$sql
= ‘select kid,kname from exam_tiku';
$data
= yii::
$app
->db->createCommand(
$sql
)->queryAll();
$arr
= Form::dataarr(
$data
);
//var_dump($arr);die;
$model
=
new
Form();
return
$this
->render(‘index
',[‘model'
=>
$model
,
'data'
=>
$arr
]);
}
public
function
actionAdd(){
$data
= Yii::
$app
->request->post();
echo
$name
=
$data
[‘Form
'][‘name'
];
}
}
然后就是在我门的view层 展示出来
<?php
/**
* Created by PhpStorm.
* User: jinlei
* Date: 2017/5/10
* Time: 9:41
*/
use
yii\helpers\Html;
use
yii\widgets\ActiveForm;
$form
= ActiveForm::begin([
‘id
' => ‘login-form'
,
‘options
' => [‘class'
=> ‘form-horizontal'],
‘action
'=>'
?r=login/add',
‘method
'=>'
post',
]) ?>
<?=
$form
->field(
$model
, ‘name') ?>
<?=
$form
->field(
$model
, ‘pwd')->passwordInput() ?>
<?=
$form
->field(
$model
, ‘sex
')->radioList([‘0'
=>
'男'
,
'1'
=>
'女'
]) ?>
<?=
$form
->field(
$model
, ‘hobby
')->checkboxList([‘basketball'
=>
'篮球'
,
'baseball'
=>
'棒球'
,
'swim'
=>
'游泳'
]) ?>
<?=
$form
->field(
$model
, ‘age')->dropDownList(
$data
) ?>
<div
class
=”form-group”>
<div
class
=”col-lg-offset-1 col-lg-11″>
<?= Html::submitButton(‘Login
', [‘class'
=> ‘btn btn-primary']) ?>
</div>
</div>
<?php ActiveForm::
end
() ?>