Controller控制器层代码

  1. <?php
  2. namespace frontend\controllers;
  3. use frontend\models\UserForm;
  4. class UserController extends \yii\web\Controller
  5. {
  6. public function actionIndex()
  7. {
  8. $model = new UserForm;
  9. if ($model->load(\Yii::$app->request->post()) && $model->validate())
  10. {
  11. echo "通过";
  12. }
  13. return $this->render('index',[
  14. "model" => $model,
  15. ]);
  16. }
  17. }

VIEWS视图层代码

  1. <?php
  2. use yii\helpers\Html;
  3. use yii\widgets\ActiveForm;
  4. ?>
  5. <h1>YII2.0使用ActiveForm</h1>
  6. <?php $form = ActiveForm::begin([
  7. 'action' => ['log/login'], //提交地址(*可省略*)
  8. 'method'=>'post',    //提交方法(*可省略默认POST*)
  9. 'id' => 'login-form', //设置ID属性
  10. 'options' => [
  11. 'class' => 'form-horizontal', //设置class属性
  12. 'enctype' => 'multipart/form-data' //文件上传时添加该编码
  13. ],
  14. 'fieldConfig' => [
  15. '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>'
  16. ],  //设置模板的样式
  17. ]); ?>
  18. <!--文本框 (*验证长度可在这里写 maxlength 这样就不用再 model 里面写了 *)-->
  19. <?= $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"=>"width:200px; height:30px;"]) ?>
  20. <!--密码框 (*不使用他的lable只需要用false把他禁止, 然后你可以自己写*)-->
  21. <h4>密码</h4><?= $form->field($model, 'pwd')->label(false)->passwordInput(['maxlength' => 20,"style"=>"width:200px; height:30px;","placeholder"=>"请输入您的密码"]) ?>
  22. <?= $form->field($model, 're_pwd')->passwordInput(['maxlength' => 20,"style"=>"width:200px; height:30px;","placeholder"=>"请输入您的密码"]) ?>
  23. <!--单选按钮(*设置默认选中*)-->
  24. <?php $model->sex=1; echo $form->field($model, 'sex')->radioList(['1'=>'男','0'=>'女']) ?>
  25. <!--验证邮箱-->
  26. <?= $form->field($model, 'email')->textInput() ?>
  27. <!--下拉框的默认选中使用 prompt 设置 -->
  28. <?= $form->field($model, 'school')->dropDownList(['1'=>'大学','2'=>'高中','3'=>'初中'], ['prompt'=>'请选择','style'=>'width:120px']) ?>
  29. <!--文件上传-->
  30. <?= $form->field($model, 'photo')->fileInput() ?>
  31. <!--复选框 -->
  32. <?= $form->field($model, 'hobby')->checkboxList(['0'=>'篮球','1'=>'足球','2'=>'羽毛球','3'=>'乒乓球']) ?>
  33. <!--文本域-->
  34. <?= $form->field($model, 'remark')->textarea(['rows'=>3]) ?>
  35. <!--隐藏域-->
  36. <?= $form->field($model, 'userid')->hiddenInput(['value'=>3])->label(false); ?>
  37. <?= Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
  38. <?= Html::resetButton('重置', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
  39. <?php ActiveForm::end(); ?>

MODELS层表单验证

  1. <?php
  2. namespace frontend\models;
  3. use Yii;
  4. class UserForm extends \yii\db\ActiveRecord
  5. {
  6. /**
  7. *@param参数
  8. */
  9. public $username;
  10. public $pwd;
  11. public $re_pwd;
  12. public $email;
  13. public $bobby;
  14. public $remark;
  15. public $photo;
  16. public $school;
  17. public $info;
  18. /**
  19. * @inheritdoc
  20. */
  21. public static function tableName()
  22. {
  23. return '{{%user}}';
  24. }
  25. /**
  26. * @inheritdoc
  27. */
  28. public function rules()
  29. {
  30. return [
  31. //验证不能为空
  32. [['username', 'pwd', 'email', 'hobby'], 'required' ,"message"=>"{attribute}不能为空"],
  33. //验证用户唯一
  34. ['username', 'unique', 'targetClass' => '\frontend\models\User', 'message' => '用户名已存在.'],
  35. //验证密码不一致
  36. ['re_pwd', 'compare', 'compareAttribute' => 'pwd', 'message' => '两次密码不一致'],
  37. //验证字符串长度
  38. [['username'],"string", "max"=>"10", "min"=>"5", "tooLong"=>"{attribute}不能大于10个字符", "tooShort"=>"{attribute}不能小于5个字符"],
  39. //验证文件上传的格式
  40. ['photo','file',
  41. 'extensions'=>['jpg','png','gif'],'wrongExtension'=>'只能上传{extensions}类型文件!',
  42. 'maxSize'=>1024*1024*2,  'tooBig'=>'文件上传过大!',
  43. 'skipOnEmpty'=>false,'uploadRequired'=>'请上传文件!',
  44. 'message'=>'上传失败!'
  45. ]
  46. //采用rules 规则验证
  47. ['email', 'email',"message"=>"{attribute}格式错误"],
  48. //方法2:
  49. //正则验证  ['tel','match','pattern'=>'/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})?$/','message'=>"{attribute}邮箱输入有误."],
  50. [['remark'], 'string', 'max' => 200],
  51. ];
  52. }
  53. /**
  54. * @inheritdoc
  55. */
  56. public function attributeLabels()
  57. {
  58. return [
  59. 'user_id' => '自增ID',
  60. 'username' => '用户名',
  61. 'pwd' => '密码',
  62. 're_pwd' => '请再次输入密码',
  63. 'sex' => '性别',
  64. 'photo' => '头像',
  65. 'email' => '邮箱',
  66. 'hobby' => '爱好',
  67. 'school' => '学校',
  68. 'remark' => '备注信息',
  69. ];
  70. }
  71. }

YII2.0使用ActiveForm表单(转)的更多相关文章

  1. yii2之ActiveForm表单使用

    因目前项目并非前后端分离模式,且用到PHP的yii2框架(所有html代码,js较多内嵌在.php文件内多少采用同步提交[喷墨中...]),遂对于前端面上需要用到的yii2小组件一些整理(因是前端若涉 ...

  2. yii2 创建ActiveForm(表单)

    表单的生成表单中的方法    ActiveForm::begin()方法    ActiveForm::end()方法    getClientOptions()方法    其它方法:errorSum ...

  3. yii中调整ActiveForm表单样式

    Yii2中对于表单和字段的支持组件为ActiveForm和ActiveField, <?php $form = ActiveForm::begin([ 'id' => 'login-for ...

  4. <玩转Django2.0>读书笔记:表单

    1. 表单字段 参考: 官方文档 Django表单字段汇总 2. 表单代码示例(forms.Form) # form.py代码 # 获取数据库数据 choices_list = [(i+1,v['ty ...

  5. 【Django笔记四】Django2.0中的表单

    一.环境版本信息: 操作系统:windows10 Django版本:2.0.5 Python版本:3.6.4 Mysql版本: 5.5.53   安装mysql 二.基础信息 1.App中的模型mod ...

  6. thinkPHP5.0使用form表单提交数据和删除文章,不用TP的提示页面,使用弹出提示信息

    form表单提交数据和删除文章时,TP的默认信息提示页面的看起来不是很好看,想要实现弹窗提示怎么做呢? 前端:可以使用前端的一个知识--iframe,iframe元素会创建包含另外一个文档的内联框架: ...

  7. yii2.0 Activeform表单部分组件使用方法

    文本框:textInput(); 密码框:passwordInput(); 单选框:radio(),radioList(); 复选框:checkbox(),checkboxList(); 下拉框:dr ...

  8. yii2.0 Activeform表单部分组件使用方法 [ 2.0 版本 ]

    文本框:textInput(); 密码框:passwordInput(); 单选框:radio(),radioList(); 复选框:checkbox(),checkboxList(); 下拉框:dr ...

  9. [moka同学笔记]yii2 activeForm 表单样式的修改(二)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABAEAAANXCAIAAADLkdErAAAgAElEQVR4nOzdfWwc953nef6zwO5Zg8

随机推荐

  1. 读取图片列表——CNN输入

    image_list = [] new_file_list = [] for root, _, file_list in os.walk(predict_dir): new_file_list += ...

  2. 1-MAVEN 仓库

    本地资源库     MAVEN的本地资源库是用来存储所有项目的依赖关系(插件和其他文件,这个文件被MAVEN 下载到本地文件中.) 可以通过修改MAVEN安装目录下conf/setting.xml配置 ...

  3. 个人前端学习路线图与github优秀前端开发者的路线图推荐

    1.个人目前学习的路线图 2.github优秀前端开发者的路线图推荐 打开github首页,在搜索框输入developer-roadmap,搜索github前端路线图 选择kamranahmedse/ ...

  4. unity3D 中的C#脚本一个类调用另一类中简单方法

    案例展示 SubMenuManage类中的实例化代码如下: static SubMenuManage sub_this; public static SubMenuManage Instance() ...

  5. bzoj3997

    题解: dp f[i][j]=max(f[i-1][j+1]+a[i][j],max(f[i-1][j],f[i][j+1])); 代码: #include<bits/stdc++.h> ...

  6. day22-python操作mysql2

    数据库连接池 python编程中可以使用MySQLdb进行数据库的连接及诸如查询/插入/更新等操作,但是每次连接mysql数据库请求时,都是独立的去请求访问,相当浪费资源,而且访问数量达到一定数量时, ...

  7. Spring Boot + Druid 监控数据库(三)

    Druid可以做什么?  1) 可以监控数据库访问性能,Druid内置提供了一个功能强大的StatFilter插件,能够详细统计SQL的执行性能,这对于线上分析数据库访问性能有帮助. 2) 替换DBC ...

  8. IEnumerable<T> list注意事项

    方法返回的时候 要设置用list会比较稳妥. 遇到的问题: private IDbConnection GetConnection(){var dataSettingsManager = new Da ...

  9. SpringMVC 接受页面传递参数

    一共是五种传参方式: 一:直接将请求参数名作为Controller中方法的形参 public  String login (String username,String password)   : 解 ...

  10. const & define & inline

    0x01  const & define区别 宏定义#define发生在预编译期,而const,enum定义的常量发生在编译期,两者的重要差别在于编译期里的变量是进符号表的,而预编译期的宏是简 ...