• YII2.0使用ActiveForm表单(转)


    Controller控制器层代码

    [php] view plain copy
     
    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
     
    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',['inputOptions' => ['placeholder'=>'请输入用户名','class' => 'ipt'],'template'=>'<div class="form-group"><div class="col-md-8 controls">{label}{input}{error}</div></div>'])->textInput(['maxlength' => 20,"style"=>"200px; height:30px;"]) ?>  
    24.       
    25.     <!--密码框 (*不使用他的lable只需要用false把他禁止, 然后你可以自己写*)-->  
    26.     <h4>密码</h4><?= $form->field($model, 'pwd')->label(false)->passwordInput(['maxlength' => 20,"style"=>"200px; height:30px;","placeholder"=>"请输入您的密码"]) ?>  
    27.   
    28.     <?= $form->field($model, 're_pwd')->passwordInput(['maxlength' => 20,"style"=>"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'=>'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
     
    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.             //验证文件上传的格式  
    48.              ['photo','file',  
    49.                 'extensions'=>['jpg','png','gif'],'wrongExtension'=>'只能上传{extensions}类型文件!',  
    50.                 'maxSize'=>1024*1024*2,  'tooBig'=>'文件上传过大!',  
    51.                 'skipOnEmpty'=>false,'uploadRequired'=>'请上传文件!',  
    52.                 'message'=>'上传失败!'  
    53.              ]  
    54.   
    55.             //采用rules 规则验证  
    56.             ['email', 'email',"message"=>"{attribute}格式错误"],   
    57.             //方法2:   
    58.             //正则验证  ['tel','match','pattern'=>'/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})?$/','message'=>"{attribute}邮箱输入有误."],  
    59.   
    60.             [['remark'], 'string', 'max' => 200],  
    61.               
    62.               
    63.         ];  
    64.     }  
    65.   
    66.     /** 
    67.      * @inheritdoc 
    68.      */  
    69.     public function attributeLabels()  
    70.     {  
    71.         return [  
    72.             'user_id' => '自增ID',  
    73.             'username' => '用户名',  
    74.             'pwd' => '密码',  
    75.              're_pwd' => '请再次输入密码',  
    76.             'sex' => '性别',  
    77.             'photo' => '头像',  
    78.             'email' => '邮箱',  
    79.             'hobby' => '爱好',  
    80.             'school' => '学校',  
    81.             'remark' => '备注信息',  
    82.         ];  
    83.     }  
    84. }  
  • 相关阅读:
    iOS仿UC浏览器顶部频道滚动效果
    OC中NSClassFromString()与NSStringFromClass()的用法及应用场景
    利用工具MailUtils实现邮件的发送,遇到的大坑,高能预警!!
    使用response实现文件下载注意点
    mac版MyEclipse的安装及创建web项目
    Mac系统下安装Tomcat,以及终端出现No such file or directory的错误提示解决方案
    机器学习笔记-Python简介
    解决mscordacwks.dll不一致问题
    IIS日志如何记录X-Forwarded-For
    深入理解Redis(番外)——持久化
  • 原文地址:https://www.cnblogs.com/shixiuxian/p/8507875.html
Copyright © 2020-2023  润新知