ThinkPHP6 is one of the most popular PHP development frameworks at present, and many PHP developers like to use it for development. During the development process, data verification is a very important part, because an excellent application must have the legality verification of user input data. In this article, we will introduce in detail how to implement data validation in ThinkPHP6.
ThinkPHP6 data validation is based on the concept of validator, that is, we need to create a validator object to validate the data. When creating a validator object, we need to specify validation rules for it and then call the validate method for verification. If verification fails, verification error information is returned, otherwise true is returned. The following is a simple example:
use thinkValidate; $data = [ 'name' => 'thinkphp', 'email' => 'thinkphp@qq.com', 'age' => 18 ]; $validate = new Validate([ 'name' => 'require|max:25', 'email' => 'email', 'age' => 'number|between:1,120', ]); if (!$validate->check($data)) { dump($validate->getError()); }
In the above code, we first define an array $data, which contains the data that needs to be verified. Then we created a validator object $validate and set three validation rules for it, namely: the name field cannot be empty and can be up to 25 characters; the email field must be a valid email address; the age field must be a number and Between 1 and 120. Finally, the check method is called to verify the data. If verification fails, an error message is output.
In the above example, we used some common verification rules, such as: require, max, email, number, between, etc. Below we will introduce some commonly used validation rules and their usage.
This rule is used to specify that the field cannot be empty. The usage is as follows:
'username' => 'require'
This rule is used to limit the maximum or minimum length of a field. The usage is as follows:
'username' => 'max:25' 'password' => 'min:8'
This rule is used to specify that the field must be a valid email address. The usage is as follows:
'email' => 'email'
This rule is used to specify that the field can only be letters or a combination of letters and numbers. The usage is as follows:
'username' => 'alpha' 'password' => 'alphaNum'
This rule is used to specify that the field must match the specified regular expression. The usage is as follows:
'username' => 'regex:^[a-z]+$'
This rule is used to specify that the field value must be unique in the database. The usage is as follows:
'email' => 'unique:user,email'
In the above example, the parameter after unique specifies that in the user table, the email field value must be unique.
This rule is used to specify that the field value must be within the specified range. The usage is as follows:
'gender' => 'in:0,1'
This rule is used to specify that the field value must be within the specified range. Usage is as follows:
'age' => 'between:1,120'
This rule is used to specify that two fields must be equal. The usage is as follows:
'password_confirm' => 'confirm:password'
In the above example, we require that the two fields password_confirm and password must be equal.
Sometimes we need to use some custom validation rules to meet specific needs. In this case, we can use the addRule method to customize the validation rules. . For example, if we want to verify that the content of a text box must contain a specified keyword, we can define a rule like this:
use thinkValidate; Validate::rule('my_rule', function($value, $rule) { return strpos($value, $rule) !== false; }); $validate = new Validate([ 'content' => 'my_rule:thinkphp' ]); if (!$validate->check($data)) { dump($validate->getError()); }
In the above code, we first registered a custom rule my_rule through the static method rule , its usage is the same as other rules. We then used this rule in the validator to verify that the value of the content field must contain the thinkphp keyword.
Sometimes we need to use different validation rules for the same field in different scenarios. For example, we use both user registration and user modification information. In each scenario, different validation rules need to be applied to the email field. At this time we can use scenario verification to meet the needs. We can specify the scene name when creating the validator object, and then set different validation rules for each scene. For example:
use thinkValidate; $data = [ 'email' => 'thinkphp@qq.com', 'password' => '123456', ]; $validate = new Validate([ 'email' => 'require|email|unique:user,email', 'password' => 'require|min:6' ]); // 假设当前为用户修改资料场景 $validate->scene('edit', function($validate) { $validate->rule('email', 'require|email'); }); if (!$validate->scene('edit')->check($data)) { dump($validate->getError()); }
In the above example, we first define a validator object $validate and set the validation rules for the email and password fields. Then, we use the scene method to specify the current scene as edit, and specify the validation rules for the email field. Finally, we call the check method to verify. If verification fails, an error message is output.
ThinkPHP6 supports multi-language verification error messages. We can achieve this by adding corresponding error messages in the validate.php file. For example, if we want to add Chinese error information to the email field, we can configure it like this:
return [ 'email' => [ 'require' => '邮箱必须填写!', 'unique' => '该邮箱已被注册!', 'email' => '邮箱格式不正确!' ] ];
This configuration file is saved in /config/validate.php, and we can obtain the corresponding error information through the getError method during verification.
The above is the basic usage of data validation in ThinkPHP6, including: validation rules, custom validation rules, scenario validation and multi-language support. Using these functions can help us more easily verify user input data and ensure the security and legality of the application. I hope this article will be helpful to all developers!
The above is the detailed content of Understanding data validation in ThinkPHP6. For more information, please follow other related articles on the PHP Chinese website!