Home  >  Article  >  Backend Development  >  Thinkphp implements automatic verification and automatic completion, _PHP tutorial

Thinkphp implements automatic verification and automatic completion, _PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:02:28968browse

Thinkphp implements automatic verification and automatic completion.

Thinkphp’s automatic verification and automatic completion are based on the content submitted by the form. After rule verification and processing of some data Insert into database.

1. Automatic verification format:

array(
  array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),
  array(验证字段2,验证规则,错误提示,[验证条件,附加规则,验证时间]),
  ......
);

Verification conditions:
self::EXISTS_VALIDATE or 0, verify if the field exists (default)
self::MUST_VALIDATE or 1 must be verified
self::VALUE_VALIDATE or 2 Validate when the value is not empty
Verification time:
Self::MODEL_INSERT or 1 verify when adding new data
self::MODEL_UPDATE or 2 verify when editing data
self::MODEL_BOTH or 3 to verify in all cases (default)

2. Auto-complete format:

array(
  array(完成字段1,完成规则,[完成条件,附加规则]),
  array(完成字段2,完成规则,[完成条件,附加规则]),
   ......
);

Completion time:
self::MODEL_INSERT or 1 Processed when adding new data (default)
self::MODEL_UPDATE or 2 Processed when updating data
self::MODEL_BOTH or 3 All cases are handled

Small instance (registration)
HTML layout:

<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">Thinkphp implements automatic verification and automatic completion, _PHP tutorial</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="Thinkphp implements automatic verification and automatic completion, _PHP tutorial"/>
      <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>

Model (MemberModel)

<&#63;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();
  }

  /* 上传Thinkphp implements automatic verification and automatic completion, _PHP tutorial */
  public function portrait() {
    if($_FILES['portrait']['name']) { // 如果上传的Thinkphp implements automatic verification and automatic completion, _PHP tutorial
      $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'];
      }
    }
  }
}

Corresponding data table structure:

Detect and insert into the database in the controller:

/* 注册 */
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();
}

The above is the entire content of this article, I hope it will be helpful to everyone’s study

Articles you may be interested in:

  • php form verification implementation code
  • php session application instance login verification
  • php cookie login verification sample code
  • PHP verification code code (latest modification, fully customized!)
  • php mobile phone number verification regular expression
  • PHP code for session sharing and login verification through session id
  • A beautiful php verification code class (share)
  • PHP generates image verification codes, click to switch instances
  • PHP uses CURL to simulate login to websites with verification codes Method

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1084601.htmlTechArticleThinkphp implements automatic verification and automatic completion. Thinkphp’s automatic verification and automatic completion are based on the content submitted by the form. , perform rule verification and processing on part of the data and then insert it into the data...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn