Example of how to use form widgets in yii 2.0, yii2.0

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.