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

Detailed explanation of the use of Yii2 form widgets

*文
Release: 2023-03-19 07:32:02
Original
1603 people have browsed it

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

<?php
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;
    }
}
Copy after login

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

<?php

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;];
}
}
Copy after login

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

<?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() ?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template