Home  >  Article  >  Backend Development  >  Detailed explanation of the use of Yii2 form widgets

Detailed explanation of the use of Yii2 form widgets

*文
*文Original
2018-01-03 13:29:121460browse

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. I hope to be helpful.

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

'用户名',
        ‘pwd'=>'密码',
        ‘sex'=>'性别',
        ‘hobby'=>'爱好',
        ‘age'=>'年龄'
        ];
    }
    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 method for converting English headers into Chinese attributeLabels

There is also our processing order Multiple selections and drop-down boxes are worth the method dataarr

Next we need to create a controller

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'];
}
}

and then display it on the view layer of our door Come out

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

‘btn btn-primary']) ?>

In this page we show the text box password box single-select multi-select drop-down box where the data of the drop-down box is read from db

Related Recommendation:

Yii2 implements QQ Internet login

Yii2 simple analysis using cache

Yii2 implements rbac permission control

The above is the detailed content of Detailed explanation of the use of Yii2 form widgets. 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