zoukankan      html  css  js  c++  java
  • [moka同学笔记]yii2.0表单的使用

    1.创建BiaodanController.php

     1 <?php
     2 /**
     3  * Created by PhpStorm.
     4  * User: moka同学
     5  * Date: 2016/08/05
     6  * Time: 10:23
     7  */
     8 namespace appcontrollers;
     9 
    10 use appmodelsBiaodan;
    11 use yiiwebController;
    12 
    13 class BiaodanController extends Controller{
    14     public function actionIndex(){
    15         print_r($_POST);die();
    16         $model = new Biaodan();
    17         //如果有表单值就需要通过验证
    18         if($model->load(Yii::$app->request->post()) && $model->validate()){
    19             return $this->render('index-two',['model'=>$model]);
    20         }else{
    21             return $this->render('index',['model'=>$model]);
    22         }
    23     }
    24 }
    25 ?>

    2.创建model,biaodan.php

     1 <?php
     2 /**
     3  * Created by PhpStorm.
     4  * User: moka同学
     5  * Date: 2016/08/05
     6  * Time: 10:26
     7  */
     8 namespace appmodels;
     9 
    10 use Yii;
    11 use yiiaseModel;
    12 class Biaodan extends Model{
    13     public $name;
    14     public $pass;
    15     public $email;
    16     public $sex;
    17     public $edu;
    18     public $hobby;
    19     public $info;
    20 
    21     public function rules()
    22     {
    23         return [
    24             [['name','pass','email','sex','edu','hobby','info'],'required'],
    25             ['email','email','message'=>'这里是邮箱'],
    26             ['name','string','length'=>[2,10]]
    27         ];
    28     }
    29 
    30     public function attributeLabels()
    31     {
    32         return [
    33             'name'=>'名称',
    34             'email'=>'邮箱',
    35             'pass'=>'密码',
    36             'edu'=>'教育',
    37             'sex'=>'性别',
    38             'hobby'=>'爱好',
    39             'info'=>'简介'
    40         ];
    41     }
    42 }
    43 ?>

    3.视图文件view/index.php

     1 <?php
     2 use yiihelpersHtml;
     3 use yiiwidgetsActiveForm;
     4 
     5 ?>
     6 <?php $form = ActiveForm::begin(); ?>
     7 <?=$form->field($model,'name')->textInput(['style'=>'200px;'])?>
     8 <?=$form->field($model,'pass')->passwordInput(['style'=>'200px;'])?>
     9 <?=$form->field($model,'email')->textInput(['style'=>'200px;'])?>
    10 <?=$form->field($model,'sex')->radioList(['1'=>'男','2'=>'女'])?>
    11 <?=$form->field($model,'edu')->dropDownList(['1'=>'大学','2'=>'中学','3'=>'小学'],['style'=>'200px;'])?>
    12 <?=$form->field($model,'hobby')->checkboxList(['篮球'=>'篮球','排球'=>'排球','乒乓球'=>'乒乓球'])?>
    13 <?=$form->field($model,'info')->textarea(['rows'=>3,'style'=>'400px;'])?>
    14 <div class="form-group">
    15     <?=Html::submitButton('提交',['class'=>'btn btn-primary'])?>
    16 </div>
    17 <?php $form = ActiveForm::end(); ?>

    index-two.php

    1 <?php
    2 use yiihelpersHtml;
    3 ?>
    4 <ul>
    5     <li><label><?=$model->name;?></label></li>   <!--//原生写法-->
    6     <li><label><?=Html::encode($model->pass)?></label></li> <!--//小部件写法-->
    7 </ul>
    我生活的地方,我为何要生活。
  • 相关阅读:
    codeforces水题100道 第十一题 Codeforces Round #143 (Div. 2) A. Team (brute force)
    codeforces水题100道 第十题 Codeforces Round #277 (Div. 2) A. Calculating Function (math)
    codeforces水题100道 第九题 Codeforces Beta Round #63 (Div. 2) Young Physicist (math)
    codeforces水题100道 第八题 Codeforces Round #274 (Div. 2) A. Expression (math)
    vs2015
    强制IE浏览器或WebBrowser控件使用指定版本显示网页
    一个基于jquery的智能提示控件intellSeach.js
    WebBrowser中html元素如何触发winform事件
    ASP.NET用DataSet导出到Excel
    ASP.NET Excel数据导入数据库
  • 原文地址:https://www.cnblogs.com/hsd1727728211/p/5740635.html
Copyright © 2011-2022 走看看