Home > Article > Backend Development > Introduction to form widgets in yii 2.0
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. Bar.
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 /** * Created by PhpStorm. * User: jinlei * Date: 2017/5/10 * Time: 9:35 */ 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'=>'用户名', ‘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 to convert English headers into Chinese attributuLabels
There is also a way for us to handle single-select, multi-select and drop-down box value dataarr
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 on the view layer of our door
<?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' => ‘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'=>'男','1'=>'女']) ?> <?= $form->field($model, ‘hobby')->checkboxList([‘basketball'=>'篮球','baseball'=>'棒球','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 display the text box password form Select multi-select drop-down box, the data of the drop-down box is read from db
The above is the detailed content of Introduction to form widgets in yii 2.0. For more information, please follow other related articles on the PHP Chinese website!