YII2.0使用ActiveForm表单
Controller控制器层代码
<?php
namespace frontend\controllers;
use frontend\models\UserForm;
class UserController extends \yii\web\Controller
{
public function actionIndex()
{
$model = new UserForm;
if ($model->load(\Yii::$app->request->post()) && $model->validate())
{
echo "通过";
}
return $this->render('index',[
"model" => $model,
]);
}
}
VIEWS视图层代码
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<h1>YII2.0使用ActiveForm</h1>
<?php $form = ActiveForm::begin([
'action' => ['log/login'], //提交地址(*可省略*)
'method'=>'post', //提交方法(*可省略默认POST*)
'id' => 'login-form', //设置ID属性
'options' => [
'class' => 'form-horizontal', //设置class属性
'enctype' => 'multipart/form-data' //文件上传时添加该编码
],
'fieldConfig' => [
'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>'
], //设置模板的样式
]); ?>
<!--文本框 (*验证长度可在这里写 maxlength 这样就不用再 model 里面写了 *)-->
<?= $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;"]) ?>
<!--密码框 (*不使用他的lable只需要用false把他禁止, 然后你可以自己写*)-->
<h4>密码</h4><?= $form->field($model, 'pwd')->label(false)->passwordInput(['maxlength' => 20,"style"=>"width:200px; height:30px;","placeholder"=>"请输入您的密码"]) ?>
<?= $form->field($model, 're_pwd')->passwordInput(['maxlength' => 20,"style"=>"width:200px; height:30px;","placeholder"=>"请输入您的密码"]) ?>
<!--单选按钮(*设置默认选中*)-->
<?php $model->sex=1; echo $form->field($model, 'sex')->radioList(['1'=>'男','0'=>'女']) ?>
<!--验证邮箱-->
<?= $form->field($model, 'email')->textInput() ?>
<!--下拉框的默认选中使用 prompt 设置 -->
<?= $form->field($model, 'school')->dropDownList(['1'=>'大学','2'=>'高中','3'=>'初中'], ['prompt'=>'请选择','style'=>'width:120px']) ?>
<!--文件上传-->
<?= $form->field($model, 'photo')->fileInput() ?>
<!--复选框 -->
<?= $form->field($model, 'hobby')->checkboxList(['0'=>'篮球','1'=>'足球','2'=>'羽毛球','3'=>'乒乓球']) ?>
<!--文本域-->
<?= $form->field($model, 'remark')->textarea(['rows'=>3]) ?>
<!--隐藏域-->
<?= $form->field($model, 'userid')->hiddenInput(['value'=>3])->label(false); ?>
<?= Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
<?= Html::resetButton('重置', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
<?php ActiveForm::end(); ?>
MODELS层表单验证
<?php
namespace frontend\models;
use Yii;
class UserForm extends \yii\db\ActiveRecord
{
/**
*@param参数
*/
public $username;
public $pwd;
public $re_pwd;
public $email;
public $bobby;
public $remark;
public $photo;
public $school;
public $info;
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%user}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
//验证不能为空
[['username', 'pwd', 'email', 'hobby'], 'required' ,"message"=>"{attribute}不能为空"],
//验证用户唯一
['username', 'unique', 'targetClass' => '\frontend\models\User', 'message' => '用户名已存在.'],
//验证密码不一致
['re_pwd', 'compare', 'compareAttribute' => 'pwd', 'message' => '两次密码不一致'],
//验证字符串长度
[['username'],"string", "max"=>"10", "min"=>"5", "tooLong"=>"{attribute}不能大于10个字符", "tooShort"=>"{attribute}不能小于5个字符"],
//验证文件上传的格式
['photo','file',
'extensions'=>['jpg','png','gif'],'wrongExtension'=>'只能上传{extensions}类型文件!',
'maxSize'=>1024*1024*2, 'tooBig'=>'文件上传过大!',
'skipOnEmpty'=>false,'uploadRequired'=>'请上传文件!',
'message'=>'上传失败!'
]
//采用rules 规则验证
['email', 'email',"message"=>"{attribute}格式错误"],
//方法2:
//正则验证 ['tel','match','pattern'=>'/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})?$/','message'=>"{attribute}邮箱输入有误."],
[['remark'], 'string', 'max' => 200],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'user_id' => '自增ID',
'username' => '用户名',
'pwd' => '密码',
're_pwd' => '请再次输入密码',
'sex' => '性别',
'photo' => '头像',
'email' => '邮箱',
'hobby' => '爱好',
'school' => '学校',
'remark' => '备注信息',
];
}
}
相关的学习网站推荐
http://www.phpxs.com/post/2327
http://www.phpxs.com/post/3441
http://www.phpxs.com/post/4378
http://www.phpxs.com/post/3443
http://www.kuitao8.com/20140425/2334.shtml
http://www.yiichina.com/tutorial/635
YII2.0使用ActiveForm表单的更多相关文章
- YII2.0使用ActiveForm表单(转)
Controller控制器层代码 <?php namespace frontend\controllers; use frontend\models\UserForm; class UserCo ...
- yii2之ActiveForm表单使用
因目前项目并非前后端分离模式,且用到PHP的yii2框架(所有html代码,js较多内嵌在.php文件内多少采用同步提交[喷墨中...]),遂对于前端面上需要用到的yii2小组件一些整理(因是前端若涉 ...
- yii2 创建ActiveForm(表单)
表单的生成表单中的方法 ActiveForm::begin()方法 ActiveForm::end()方法 getClientOptions()方法 其它方法:errorSum ...
- yii中调整ActiveForm表单样式
Yii2中对于表单和字段的支持组件为ActiveForm和ActiveField, <?php $form = ActiveForm::begin([ 'id' => 'login-for ...
- <玩转Django2.0>读书笔记:表单
1. 表单字段 参考: 官方文档 Django表单字段汇总 2. 表单代码示例(forms.Form) # form.py代码 # 获取数据库数据 choices_list = [(i+1,v['ty ...
- 【Django笔记四】Django2.0中的表单
一.环境版本信息: 操作系统:windows10 Django版本:2.0.5 Python版本:3.6.4 Mysql版本: 5.5.53 安装mysql 二.基础信息 1.App中的模型mod ...
- thinkPHP5.0使用form表单提交数据和删除文章,不用TP的提示页面,使用弹出提示信息
form表单提交数据和删除文章时,TP的默认信息提示页面的看起来不是很好看,想要实现弹窗提示怎么做呢? 前端:可以使用前端的一个知识--iframe,iframe元素会创建包含另外一个文档的内联框架: ...
- yii2.0 Activeform表单部分组件使用方法
文本框:textInput(); 密码框:passwordInput(); 单选框:radio(),radioList(); 复选框:checkbox(),checkboxList(); 下拉框:dr ...
- yii2.0 Activeform表单部分组件使用方法 [ 2.0 版本 ]
文本框:textInput(); 密码框:passwordInput(); 单选框:radio(),radioList(); 复选框:checkbox(),checkboxList(); 下拉框:dr ...
- [moka同学笔记]yii2 activeForm 表单样式的修改(二)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABAEAAANXCAIAAADLkdErAAAgAElEQVR4nOzdfWwc953nef6zwO5Zg8
随机推荐
- 【PostgreSql】more than one owned sequence found
do --check seq not in sync $$ declare _r record; _i bigint; _m bigint; begin for _r in ( Select DIST ...
- flink 启动job命令
0. 启动flink-session ./bin/yarn-session.sh -n 4 -s 3 -jm 2048 -tm 6144 高版本 bin/yarn-session.sh -d -s 3 ...
- 微信内置浏览器的JsAPI(WeixinJSBridge)
参考: https://www.baidufe.com/item/f07a3be0b23b4c9606bb.html https://github.com/zxlie/WeixinApi
- mssql实现Split
create function Fun_Split( @SourceSql varchar ( 8000 ), @StrSeprate varchar ( 10 )) returns @temp ta ...
- 关系类处理专题之创建关系类|RelationShipClass
/// <summary> /// 存在于数据库中的数据集中 /// </summary> /// <param name="mdbPath"> ...
- python-GUI-pyqt5之文件加密解密工具
pyqt5的文件加密解密程序,用到base64,rsa和aes进行混合解密,代码比较杂乱,可自行整理,仅供学习参考之用,如需转载,请联系博主或附上博客链接,下面直接干货. 程序截图如下: # -*- ...
- sql 查询大数据 常用 50列优化
大数据量的问题是很多面试笔试中经常出现的问题,比如baidu google 腾讯 这样的一些涉及到海量数据的公司经常会问到. 下面的方法是我对海量数据的处理方法进行了一个一般性的总结,当然这些方法可能 ...
- Weak Encryption 弱加密安全问题处理
Weak Encryption Abstract 程序使用了弱加密算法,无法保证敏感数据的保密性. Explanation 陈旧的加密算法(如 DES)再也不能为敏感数据提供足够的保护了. 加密算法依 ...
- java 面向对象 --static
java 面向对象 --static package charpter5.Demo09; //static public class Student { private static int age; ...
- AlphaFold2中的残基刚体表示
技术背景 在前面的这一篇博客中,比较全面的介绍了组成蛋白质的各种氨基酸的三维结构.由于每个氨基酸大小不一,在传统的蛋白质折叠预测的方案中,一般会考虑全原子方案或者是粗粒化方案.对于全原子方案而言,即时 ...