Home >Backend Development >PHP Tutorial >Introduction to form widgets in yii 2.0

Introduction to form widgets in yii 2.0

巴扎黑
巴扎黑Original
2017-08-13 11:32:001190browse

This article mainly introduces you to the use of form widgets in Yii 2.0. The introduction in the article is very detailed and has certain reference learning value for everyone's study or work. Friends who need it can take a look below. Bar.

Preface

This article mainly introduces the relevant content about the use of form widgets in yii 2.0. It is shared for your reference and study. Below Let’s take a look at the detailed introduction:

Usage method

First create the model layer. Because you want to use form widgets, you need to load the corresponding components. , the components required here are yii\widgets\ActiveForm yii\helpers\Html

Next, write the method in the class defined by the model. First, we need to define the name value that needs to be used in the form widget

Not much to say about the code


<?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&#39;=>&#39;用户名&#39;,
‘pwd&#39;=>&#39;密码&#39;,
‘sex&#39;=>&#39;性别&#39;,
‘hobby&#39;=>&#39;爱好&#39;,
‘age&#39;=>&#39;年龄&#39;
];
}
static public function dataarr($data){
$arr = array();
foreach($data as $key=>$value){
$arr[$value[‘kid&#39;]] = $value[‘kname&#39;];
}
return $arr;
}
}

In this model, there is a method to convert English headers into Chinese attributuLabels

There is also a way for us to handle single-select, multi-select and drop-down box value dataarr

Next we need to create a 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&#39;;
$data = yii::$app->db->createCommand($sql)->queryAll();
$arr = Form::dataarr($data);
//var_dump($arr);die;
$model = new Form();
return $this->render(‘index&#39;,[‘model&#39;=>$model,&#39;data&#39;=>$arr]);
}
public function actionAdd(){
$data = Yii::$app->request->post();
echo $name = $data[‘Form&#39;][‘name&#39;];
}
}

Then it is displayed on the view layer of our door


<?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&#39; => ‘login-form&#39;,
‘options&#39; => [‘class&#39; => ‘form-horizontal&#39;],
‘action&#39;=>&#39;?r=login/add&#39;,
‘method&#39;=>&#39;post&#39;,
]) ?>
<?= $form->field($model, ‘name&#39;) ?>
<?= $form->field($model, ‘pwd&#39;)->passwordInput() ?>
<?= $form->field($model, ‘sex&#39;)->radioList([‘0&#39;=>&#39;男&#39;,&#39;1&#39;=>&#39;女&#39;]) ?>
<?= $form->field($model, ‘hobby&#39;)->checkboxList([‘basketball&#39;=>&#39;篮球&#39;,&#39;baseball&#39;=>&#39;棒球&#39;,&#39;swim&#39;=>&#39;游泳&#39;]) ?>
<?= $form->field($model, ‘age&#39;)->dropDownList($data) ?>

<p class=”form-group”>
<p class=”col-lg-offset-1 col-lg-11″>
<?= Html::submitButton(‘Login&#39;, [‘class&#39; => ‘btn btn-primary&#39;]) ?>
</p>
</p>
<?php ActiveForm::end() ?>

In this page we display the text box password form Select multi-select drop-down box, the data of the drop-down box is read from db

The above is the detailed content of Introduction to form widgets in yii 2.0. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn