yii2表单使用

来源:互联网 发布:rgb转换为cmy算法 编辑:程序博客网 时间:2024/05/16 08:35

controller
  1. <?php  
  2.   
  3. namespace frontend\controllers;  
  4.   
  5. use frontend\models\UserForm;  
  6. class UserController extends \yii\web\Controller  
  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视图层代码
  1. <?php  
  2. use yii\helpers\Html;  
  3. use yii\widgets\ActiveForm;  
  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层表单验证
  1. <?php  
  2.   
  3. namespace frontend\models;  
  4.   
  5. use Yii;  
  6. class UserForm extends \yii\db\ActiveRecord  
  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' => '\frontend\models\User''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.     }  

0 1
原创粉丝点击