This article mainly introduces to you about the use of the form small parts of Yii 2.0, the article introduced in very detailed, to everyone's study or work has a certain reference learning value, the need for friends to see together below. We hope to help you.
Objective
This article is mainly about Yii 2.0 in the form of the use of small parts of the relevant content, shared out for everyone to refer to the study, the following to see the detailed introduction:
How to use
First create the model layer, because to use the form widget, so to load the corresponding components, the components required here are Yii\widgets\activeform yii\helpers\html
Next we write the method in the model definition class, first we define the name value that needs to use the form widget
Not much to say on the code
<?phpnamespace 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 ' = ' username ', ' pwd ' = ' password ', ' sex ' = ' gender ', ' hobby ' = ' hobby ', ' age ' = ' ages ' ; } static public Function Dataarr ($data) { $arr = array (); foreach ($data as $key = + $value) { $arr [$value [' kid ']] = $value [' Kname ']; } return $arr; }}
In this model there is a way to convert English headers into Chinese attributulabels
And we're dealing with a single-choice multi-Select and a drop-down box worth the way Dataarr
Next we need to create a controller
<?phpnamespace 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:: $a Pp->db->createcommand ($sql)->queryall () $arr = Form::d ataarr ($data);//var_dump ($arr);d ie; $model = new Form return $this->render (' Index ', [' model ' = ' $model, ' data ' = $arr]); Public Function Actionadd () {$data = Yii:: $app->request->post (); echo $name = $data [' Form '] [' name '];}}
And then it's on the view level of my door.
<?php/*** Created by phpstorm.* user:jinlei* date:2017/5/10* time:9:41*/use yii\helpers\html;use yii\widgets\Active Form; $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 ' = ' male ', ' 1 ' + ') Female '])? ><?= $form->field ($model, ' hobby ')->checkboxlist ([' basketball ' = ' basketball ', ' baseball ' = ' baseball ', ' Swim ' = ' swim ']? ><?= $form->field ($model, ' age ')->dropdownlist ($data)? ><p class= "Form-group" ><p class= "col-lg-offset-1 col-lg-11″><?= Html::submitbutton (' Login ', [' class ' = ' btn btn-primary '])? ></p></p><?php activeform::end ()?>
In this page we show the text box password box single-select multi-select dropdown box where the data for the drop-down box is read from the DB
Related recommendations:
Yii2 Realize QQ Interconnection Login
Yii2 Simple parsing using caching
YII2 realization of RBAC permission control