Example of how to use form widgets in yii 2.0, yii2.0
Preface
This article mainly introduces the use of form widgets in yii 2.0. For your reference, let's take a look at the details below:
Usage
First, create the model layer. To use form widgets, load the corresponding components. The required components include 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 of the form widget.
Not to mention the code
<? Php/*** Created by PhpStorm. * User: jinlei * Date: * Time: */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 $ holobby; public $ age; public function rules () {return [];} public function attributeLabels () {return ['name' => 'username', 'pwd' => 'Password', 'sex' => 'gender ', 'hobby' => 'hobbies ', 'age' => 'age'];} static public function dataarr ($ data) {$ arr = array (); foreach ($ data as $ key =>$ value) {$ arr [$ value ['kid'] = $ value ['kname'] ;}return $ arr ;}}
In this model, attributuLabels is used to convert the English header to Chinese.
There is also dataarr, which can be used to process multiple choice and drop-down boxes.
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';$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'];}}
Then it is displayed at the view layer of our door.
<? Php/*** Created by PhpStorm. * User: jinlei * Date: * Time: */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' => 'male', '1' => 'female])?> <? = $ Form-> field ($ model, 'hobby')-> checkboxList (['bucketball '=> 'basketball', 'baseball' => 'baseball ', 'swim '=> 'swing'])?> <? = $ 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> <? Php ActiveForm: end ()?>
On this page, we show that the data in the drop-down box in the password box of the text box is read from the db.
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.