Thinkphp实现自动验证和自动完成,
Thinkphp实现自动验证和自动完成,
Thinkphp的自动验证和自动完成都是根所表单提交的内容来的,对部分数据进行规则验证和处理后插入到数据库。
1、自动验证格式:
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、自动完成格式:
array( array(完成字段1,完成规则,[完成条件,附加规则]), array(完成字段2,完成规则,[完成条件,附加规则]), ...... );
完成时间:
self::MODEL_INSERT或者1 新增数据的时候处理(默认)
self::MODEL_UPDATE或者2 更新数据的时候处理
self::MODEL_BOTH或者3 所有情况都进行处理
小实例(注册)
HTML布局:
<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="max-width:90%" 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)
<?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']; } } } }
对应的数据表结构:
在控制器中检测及插入到数据库:
/* 注册 */ 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(); }
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
- php 表单验证实现代码
- php session应用实例 登录验证
- php cookie 登录验证示例代码
- PHP验证码类代码( 最新修改,完全定制化! )
- php 手机号码验证正则表达式
- PHP通过session id 实现session共享和登录验证的代码
- 一个漂亮的php验证码类(分享)
- PHP生成图片验证码、点击切换实例
- PHP使用CURL实现对带有验证码的网站进行模拟登录的方法

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
