search
HomePHP FrameworkThinkPHPHow to use the modification function of ThinkPHP automatic verification

1. Introduction to ThinkPHP automatic verification

ThinkPHP automatic verification is a set of verification mechanisms built into the framework. It can set verification rules and errors in the model Prompt information, and perform data verification based on actual scenarios in the controller. Using automatic verification can avoid the tedious manual verification process and improve development efficiency. With automatic validation, error handling code can be reduced because it will directly return an error message when the data is illegal.

The basic usage of ThinkPHP automatic verification is as follows:

  1. Define validation rules and error messages in the model:

protected $_validate = array(
    // 验证用户名是否合法
    array('username','require','用户名不能为空!'),
    array('username','','该用户名已被注册',0,'unique'),
    array('username','/^[\w\-\x{4e00}-\x{9fa5}]{2,16}$/','用户名不合法!',0,'regex'),
    // 验证邮箱是否合法
    array('email','require','电子邮箱不能为空!'),
    array('email','','该邮箱已被注册',0,'unique'),
    array('email','email','电子邮箱格式不正确!',0,'regex'),
    // 验证密码是否合法
    array('password','require','密码不能为空!'),
    array('password','/^[\S]{6,32}$/','密码格式不正确!',0,'regex'),
);
  1. Data validation in the controller:

public function register(){
    if(IS_POST){
        $user = D('User');
        if(!$user->create()){
            $this->ajaxReturn(array('status'=>0,'msg'=>$user->getError()));
        }else{
            $user->add();
            $this->ajaxReturn(array('status'=>1,'msg'=>'注册成功!'));
        }
    }
}

In the above code, we use $user->create() Data verification, if the verification fails, use $user->getError() to obtain the error information and return it to the front-end page. If the validation is successful, the data is added to the database.

2. ThinkPHP automatic verification modification

In the actual development process, sometimes some fields need to be updated, but in this case data also needs to be updated verify. Although we can use an automatic verification mechanism, this mechanism will verify all verification rules again, resulting in a waste of time and resources.

In order to solve this problem, ThinkPHP provides an automatic verification modification function, which can only verify the fields that need to be verified according to the current scenario. If you want to modify the username and email fields in the database without verifying password, you can use the following code:

public function update(){
    if(IS_POST){
        $user = D('User');
        $data = array(
            'id' => $_POST['id'],
            'username' => $_POST['username'],
            'email' => $_POST['email'],
        );
        if(!$user->create($data, 2)){
            $this->ajaxReturn(array('status'=>0,'msg'=>$user->getError()));
        }else{
            $user->save();
            $this->ajaxReturn(array('status'=>1,'msg'=>'更新成功!'));
        }
    }
}

In the above code, we passed the second parameter 2, indicating that the current update scene is. In this way, in the create() method, the framework will only verify username and email, but not other fields.

The above is the detailed content of How to use the modification function of ThinkPHP automatic verification. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!