Home  >  Article  >  PHP Framework  >  How to perform form validation in ThinkPHP6?

How to perform form validation in ThinkPHP6?

WBOY
WBOYOriginal
2023-06-12 09:36:261590browse

ThinkPHP6 is a PHP-based MVC framework that greatly simplifies the development of web applications. Among them, form validation is a very basic and important function. In this article, we will introduce how to perform form validation operations in ThinkPHP6.

1. Validation rule definition
In ThinkPHP6, validation rules need to be defined in the controller. We can define the rules by defining a $validate attribute in the controller, as shown below:

use thinkValidate;
class UserController extends Controller {
    protected $validate;
    public function __construct(Validate $validate) {
        $this->validate = $validate;
    }

    // 定义验证规则
    protected $rule = [
        'name' => 'require|max:25',
        'email' => 'email',
        'age' => 'number|between:1,120',
    ];
}

2. Form Validation
After we define the validation rules, we can use the check() method of $validate in the controller to execute the validation rules we defined. The verification operation can be implemented by calling the validate() method on the $request object in the controller and passing in the verification rules.

public function add(Request $request) {
    $data = $request->param();
    $result = $this->validate($data, $this->rule);
    if ($result !== true) {
        return ['code' => 1, 'msg' => $result];
    }
    // 验证通过,执行添加操作
}

In the above code, the $request->param() method returns an array that contains all parameter values ​​in the form. We pass it into the validate() method for verification. If the verification fails, an error message will be returned. If the verification passes, the addition operation will be performed directly.

3. Custom error messages
In actual development, we may need to set custom error messages for some rules. This can be achieved by using the message() method in the verification rules.

protected $rule = [
    'name' => 'require|max:25',
    'email' => 'email',
    'age' => 'number|between:1,120',
];

protected $message = [
    'name.require' => '用户名必填',
    'name.max' => '用户名最多不能超过25个字符',
    'email.email' => '邮箱格式错误',
    'age.number' => '年龄必须是数字',
    'age.between' => '年龄必须在1~120之间',
];

By using the message() method of $validate in the controller, we can customize the error message of the rule. For example:

$result = $this->validate($data, $this->rule, $this->message);

4. Batch verification
When we need to verify multiple forms, we can use the batch() method of $validate for batch verification. For example:

public function verify(Request $request) {
    $data = $request->param();
    $rule = [
        'name' => 'require|max:25',
        'email' => 'email',
        'age' => 'number|between:1,120',
    ];
    $message = [
        'name.require' => '用户名必填',
        'name.max' => '用户名最多不能超过25个字符',
        'email.email' => '邮箱格式错误',
        'age.number' => '年龄必须是数字',
        'age.between' => '年龄必须在1~120之间',
    ];
    $result = $this->validate($data, $rule, $message, true);
    if ($result !== true) {
        return ['code' => 1, 'msg' => $result];
    }
    // 验证通过,执行相关操作
}

In the above code, the fourth parameter of the $validate method is true, which means batch verification is enabled. After turning on batch verification, you can set multiple form verification rules to return all error messages when verification fails.

Summary:
In ThinkPHP6, validation rules are defined by defining the $validate attribute in the controller, form validation is performed by calling the validate() method on the $request object, and message() can be used Method to set custom error messages. At the same time, batch verification can be achieved through the batch() method. These operations are very basic and commonly used, and we must master them proficiently in actual development.

The above is the detailed content of How to perform form validation in ThinkPHP6?. For more information, please follow other related articles on the PHP Chinese website!

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