入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

yii2表单使用

创建时间:2016-03-29 投稿人: 浏览次数:2108

Controller控制器层代码

[php] view plain copy  print?在CODE上查看代码片派生到我的代码片
  1. <?php  
  2.   
  3. namespace frontendcontrollers;  
  4.   
  5. use frontendmodelsUserForm;  
  6. class UserController extends yiiwebController  
  7. {  
  8.     public function actionIndex()  
  9.     {  
  10.         $model = new UserForm;  
  11.         if ($model->load(Yii::$app->request->post()) && $model->validate())  
  12.         {  
  13.             echo "通过";  
  14.         }  
  15.           
  16.         return $this->render("index",[  
  17.             "model" => $model,     
  18.         ]);  
  19.     }  
  20.   
  21. }  

VIEWS视图层代码

[php] view plain copy  print?在CODE上查看代码片派生到我的代码片
  1. <?php  
  2. use yiihelpersHtml;  
  3. use yiiwidgetsActiveForm;  
  4.   
  5. ?>  
  6.   
  7. <h1>YII2.0使用ActiveForm</h1>  
  8.   
  9. <?php $form = ActiveForm::begin([  
  10.     "action" => ["log/login"], //提交地址(*可省略*)  
  11.     "method"=>"post",    //提交方法(*可省略默认POST*)  
  12.     "id" => "login-form", //设置ID属性  
  13.     "options" => [  
  14.             "class" => "form-horizontal", //设置class属性  
  15.             "enctype" => "multipart/form-data" //文件上传时添加该编码  
  16.     ],  
  17.     "fieldConfig" => [  
  18.             "template" => "<div class="form-group"><center><label class="col-md-2 control-label" for="type-name-field">{label}</label></center><div class="col-md-8 controls">{input}{error}</div></div>"  
  19.     ],  //设置模板的样式  
  20. ]); ?>  
  21.   
  22.     <!--文本框 (*验证长度可在这里写 maxlength 这样就不用再 model 里面写了 *)-->  
  23.     <?= $form->field($model, "username",["template"=>"<div class="form-group"><div class="col-md-8 controls">{label}{input}{error}</div></div>"])->textInput(["maxlength" => 20,"style"=>"width:200px; height:30px;"]) ?>  
  24.       
  25.     <!--密码框 (*不使用他的lable只需要用false把他禁止, 然后你可以自己写*)-->  
  26.     <h4>密码</h4><?= $form->field($model, "pwd")->label(false)->passwordInput(["maxlength" => 20,"style"=>"width:200px; height:30px;","placeholder"=>"请输入您的密码"]) ?>  
  27.   
  28.     <?= $form->field($model, "re_pwd")->passwordInput(["maxlength" => 20,"style"=>"width:200px; height:30px;","placeholder"=>"请输入您的密码"]) ?>  
  29.       
  30.     <!--单选按钮(*设置默认选中*)-->  
  31.     <?php $model->sex=1; echo $form->field($model, "sex")->radioList(["1"=>"男","0"=>"女"]) ?>  
  32.   
  33.     <!--验证邮箱-->  
  34.     <?= $form->field($model, "email")->textInput() ?>  
  35.       
  36.     <!--下拉框的默认选中使用 prompt 设置 -->  
  37.     <?= $form->field($model, "school")->dropDownList(["1"=>"大学","2"=>"高中","3"=>"初中"], ["prompt"=>"请选择","style"=>"width:120px"]) ?>  
  38.       
  39.     <!--文件上传-->  
  40.     <?= $form->field($model, "photo")->fileInput() ?>  
  41.       
  42.     <!--复选框 -->  
  43.     <?= $form->field($model, "hobby")->checkboxList(["0"=>"篮球","1"=>"足球","2"=>"羽毛球","3"=>"乒乓球"]) ?>  
  44.       
  45.     <!--文本域-->  
  46.     <?= $form->field($model, "remark")->textarea(["rows"=>3]) ?>  
  47.       
  48.     <!--隐藏域-->  
  49.     <?= $form->field($model, "userid")->hiddenInput(["value"=>3])->label(false); ?>  
  50.   
  51.     <?= Html::submitButton("提交", ["class"=>"btn btn-primary","name" =>"submit-button"]) ?>  
  52.          
  53.     <?= Html::resetButton("重置", ["class"=>"btn btn-primary","name" =>"submit-button"]) ?>  
  54.   
  55. <?php ActiveForm::end(); ?>  

MODELS层表单验证

[php] view plain copy  print?在CODE上查看代码片派生到我的代码片
  1. <?php  
  2.   
  3. namespace frontendmodels;  
  4.   
  5. use Yii;  
  6. class UserForm extends yiidbActiveRecord  
  7. {  
  8.     /** 
  9.     *@param参数 
  10.     */  
  11.     public $username;  
  12.     public $pwd;  
  13.     public $re_pwd;  
  14.     public $email;  
  15.     public $bobby;  
  16.     public $remark;  
  17.     public $photo;  
  18.     public $school;  
  19.     public $info;  
  20.   
  21.     /** 
  22.      * @inheritdoc 
  23.      */  
  24.     public static function tableName()  
  25.     {  
  26.         return "{{%user}}";  
  27.     }  
  28.   
  29.     /** 
  30.      * @inheritdoc 
  31.      */  
  32.     public function rules()  
  33.     {  
  34.         return [  
  35.             //验证不能为空  
  36.             [["username", "pwd", "email", "hobby"], "required" ,"message"=>"{attribute}不能为空"],   
  37.               
  38.             //验证用户唯一  
  39.             ["username", "unique", "targetClass" => "frontendmodelsUser", "message" => "用户名已存在."],  
  40.   
  41.             //验证密码不一致  
  42.             ["re_pwd", "compare", "compareAttribute" => "pwd", "message" => "两次密码不一致"],  
  43.   
  44.             //验证字符串长度  
  45.             [["username"],"string", "max"=>"10", "min"=>"5", "tooLong"=>"{attribute}不能大于10个字符", "tooShort"=>"{attribute}不能小于5个字符"],  
  46.   
  47.             //采用rules 规则验证  
  48.             ["email", "email","message"=>"{attribute}格式错误"],   
  49.             //方法2:   
  50.             //正则验证  ["tel","match","pattern"=>"/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})?$/","message"=>"{attribute}邮箱输入有误."],  
  51.   
  52.             [["remark"], "string", "max" => 200],  
  53.               
  54.               
  55.         ];  
  56.     }  
  57.   
  58.     /** 
  59.      * @inheritdoc 
  60.      */  
  61.     public function attributeLabels()  
  62.     {  
  63.         return [  
  64.             "user_id" => "自增ID",  
  65.             "username" => "用户名",  
  66.             "pwd" => "密码",  
  67.              "re_pwd" => "请再次输入密码",  
  68.             "sex" => "性别",  
  69.             "email" => "邮箱",  
  70.             "hobby" => "爱好",  
  71.             "school" => "学校",  
  72.             "remark" => "备注信息",  
  73.         ];  
  74.     }  
  75. }  
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。