Home > PHP Framework > ThinkPHP > body text

ThinkPHP6 form validation and data validation: ensuring the legality of data

王林
Release: 2023-08-26 13:55:44
Original
1049 people have browsed it

ThinkPHP6 form validation and data validation: ensuring the legality of data

ThinkPHP6 form validation and data validation: ensuring the legality of data

In the process of web application development, form validation ensures the legality and integrity of data An important part. The ThinkPHP6 framework provides powerful form validation and data validation functions, which can simplify the development process and help us reduce the occurrence of errors and vulnerabilities.

1. Form validation

  1. Validation rule declaration

ThinkPHP6 supports the use of annotations to declare validation rules for the controller's request method. We can declare validation rules using the @validate annotation on the controller's request method. Specific validation rules can be specified by creating a validator, or written directly in annotations.

use thinknnotationalidate;

class UserController
{
    /**
     * @validate('UserValidate.login')
     */
    public function login()
    {
        // ...
    }
}
Copy after login
  1. Validator definition

Create a validator class to define specific validation rules. You can quickly create a validator through the command line:

php think make:validate UserValidate
Copy after login

Then write the validation rules in the generated UserValidate.php file:

namespace appalidate;

use thinkValidate;

class UserValidate extends Validate
{
    protected $rule = [
        'username' => 'require',
        'password' => 'require',
        'captcha' => 'require|captcha'
    ];

    protected $message = [
        'username.require' => '用户名不能为空',
        'password.require' => '密码不能为空',
        'captcha.require' => '验证码不能为空',
        'captcha.captcha' => '验证码不正确'
    ];
}
Copy after login
  1. Validation error handling

In the controller, we can use validate method for verification. If the verification fails, a ValidateException exception will be thrown. We can handle the error by catching the exception.

try {
    $this->validate($data, 'appalidateUserValidate.login');
} catch (ValidateException $e) {
    // 验证不通过,输出错误信息
    dump($e->getError());
}
Copy after login

2. Data verification

In addition to verifying the form, ThinkPHP6 also provides a wealth of data verification methods that can verify database data.

  1. Custom validation rules

We can define custom validation rules by creating a validator class, just create a method in the validator class. For example, we define a validation rule to check whether the user name is unique:

namespace appalidate;

use thinkValidate;
use appmodelUser;

class UserValidate extends Validate
{
    // ...

    // 自定义验证规则
    protected function uniqueUsername($value, $rule, $data)
    {
        $user = User::where('username', $value)->find();
        if ($user) {
            return '用户名已存在';
        }
        return true;
    }
}
Copy after login
  1. Data validation

Data validation can be done in the model class, we can do it by Validation rules are defined in the validate method.

namespace appmodel;

use thinkModel;

class User extends Model
{
    // 定义验证规则
    protected $validate = [
        'username' => 'require|uniqueUsername:appalidateUserValidate',
        'password' => 'require'
    ];
    
    // ...
}
Copy after login

Then, use the validate method in the controller to validate the data:

$user = new User;
$user->save($data);
if ($user->validate(true)->save()) {
    // 数据验证通过,保存数据
} else {
    // 验证不通过,输出错误信息
    dump($user->getError());
}
Copy after login

Through the above method, we can easily perform form validation and data validation, ensuring Data legality and integrity. The verification function of ThinkPHP6 provides us with a convenient and safe data verification and processing mechanism, which greatly simplifies the development process and reduces the occurrence of errors and vulnerabilities.

The above is the detailed content of ThinkPHP6 form validation and data validation: ensuring the legality of data. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template