Thinkphp的自动验证和自动完成都是根所表单提交的内容来的,对部分数据进行规则验证和处理后插入到数据库。
1、自动验证格式:
1
2
3
4
5
|
array ( array (验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]), array (验证字段2,验证规则,错误提示,[验证条件,附加规则,验证时间]), ...... ); |
验证条件:
self::EXISTS_VALIDATE 或者0 存在字段就验证(默认)
self::MUST_VALIDATE 或者1 必须验证
self::VALUE_VALIDATE或者2 值不为空的时候验证
验证时间:
self::MODEL_INSERT或者1新增数据时候验证
self::MODEL_UPDATE或者2编辑数据时候验证
self::MODEL_BOTH或者3全部情况下验证(默认)
2、自动完成格式:
1
2
3
4
5
|
array ( array (完成字段1,完成规则,[完成条件,附加规则]), array (完成字段2,完成规则,[完成条件,附加规则]), ...... ); |
完成时间:
self::MODEL_INSERT或者1 新增数据的时候处理(默认)
self::MODEL_UPDATE或者2 更新数据的时候处理
self::MODEL_BOTH或者3 所有情况都进行处理
小实例(注册)
HTML布局:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
< form class = "form-horizontal" action = "{:U('Login/register')}" method = "post" autocomplete = "off" enctype = "multipart/form-data" > < div class = "form-group" > < label class = "col-lg-2 control-label" >用户名</ label > < div class = "col-lg-4" > < input class = "form-control" type = "text" name = "username" /> </ div > </ div > < div class = "form-group" > < label class = "col-lg-2 control-label" >密码</ label > < div class = "col-lg-4" > < input class = "form-control" type = "password" name = "password" /> </ div > </ div > < div class = "form-group" > < label class = "col-lg-2 control-label" >重复密码</ label > < div class = "col-lg-4" > < input class = "form-control" type = "password" name = "repassword" /> </ div > </ div > < div class = "form-group" > < label class = "col-lg-2 control-label" >头像</ label > < div class = "col-lg-4" > < input class = "form-control" type = "file" name = "portrait" id = "imgpath" /> < img style = "width: 100%;" src = "" id = "showimgpath" alt = "头像" /> < span class = "help-block" >关像的大小为80*80px</ span > </ div > </ div > < div class = "form-group" > < label class = "col-lg-2 control-label" >性别</ label > < div class = "col-lg-4" > < div class = "btn-group" data-toggle = "buttons" > < label class = "btn btn-default active" > < input type = "radio" name = "gender" autocomplete = "off" value = "1" checked /> 男 </ label > < label class = "btn btn-default" > < input type = "radio" name = "gender" autocomplete = "off" value = "0" /> 女 </ label > </ div > </ div > </ div > < div class = "form-group" > < label class = "col-lg-2 control-label" >电话号码</ label > < div class = "col-lg-4" > < input class = "form-control" type = "input" name = "phone" /> </ div > </ div > < div class = "form-group" > < label class = "col-lg-2 control-label" >邮箱</ label > < div class = "col-lg-4" > < input class = "form-control" type = "input" name = "email" /> </ div > </ div > < div class = "form-group" > < div class = "col-lg-2 col-lg-offset-2" > < button class = "btn btn-primary btn-block btn-submit" type = "submit" >注册</ button > </ div > </ div > </ form > |
模型(MemberModel)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
<?php namespace Admin\Model; use Think\Model; class MemberModel extends Model { /* 自动验证 */ protected $_validate = array ( array ( 'username' , '' , '用户名是唯一的!' , self::EXISTS_VALIDATE, 'unique' , self::MODEL_INSERT), array ( 'password' , 'require' , '没有填写密码!' , self::EXISTS_VALIDATE, '' , self::MODEL_INSERT), array ( 'repassword' , 'password' , '重复密码不正确!' , self::EXISTS_VALIDATE, 'confirm' , self::MODEL_INSERT), array ( 'phone' , '11' , '电话号码长度不对!' , self::EXISTS_VALIDATE, 'length' , self::MODEL_INSERT), array ( 'email' , 'email' , '邮箱格式不正确!' ,self::EXISTS_VALIDATE, '' , self::MODEL_INSERT) ); /* 自动完成 */ protected $_auto = array ( array ( 'password' , 'encrypt' , self::MODEL_INSERT, 'callback' ), array ( 'state' , '1' ,self::MODEL_INSERT), array ( 'portrait' , 'portrait' , self::MODEL_INSERT, 'callback' ), array ( 'create_time' , 'createTime' , self::MODEL_INSERT, 'callback' ) ); /* 给密码加密 */ public function encrypt() { return md5(crypt(I( 'post.password/s' ), 'zh' )); } /* 创建时间 */ public function createTime() { return time(); } /* 上传头像 */ public function portrait() { if ( $_FILES [ 'portrait' ][ 'name' ]) { // 如果上传的头像 $upload = new \Think\Upload(); // 实例化上传类 $upload ->maxSize = 3145728 ; // 设置附件上传大小 $upload ->exts = array ( 'jpg' , 'gif' , 'png' , 'jpeg' ); // 设置附件上传类型 $upload ->rootPath = './Uploads/portrait/' ; // 设置附件上传根目录 // 上传单个文件 $info = $upload ->uploadOne( $_FILES [ 'portrait' ]); if (! $info ) { // 上传错误提示错误信息 $this ->error( $upload ->getError()); } else { // 上传成功 获取上传文件信息 $portraitPath = './Uploads/portrait/' . $info [ 'savepath' ]. $info [ 'savename' ]; $image = new \Think\Image(); $image ->open( $portraitPath ); // 生成一个居中裁剪为80*80的缩略图 $image ->thumb(150, 150,\Think\Image::IMAGE_THUMB_CENTER)->save( $portraitPath ); return $info [ 'savepath' ]. $info [ 'savename' ]; } } } } |
对应的数据表结构:
在控制器中检测及插入到数据库:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/* 注册 */ public function register() { if (IS_POST) { $member = D( 'member' ); if ( $member ->create()) { if ( $member ->add()) { $this ->success( '注册成功!' ); } else { $this ->error( '注册失败!' ); } } else { exit ( $member ->getError()); } } $this ->display(); } |
以上就是本文的全部内容,希望对大家的学习有所帮助