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
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() { // ... } }
Create a validator class to define specific validation rules. You can quickly create a validator through the command line:
php think make:validate UserValidate
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' => '验证码不正确' ]; }
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()); }
2. Data verification
In addition to verifying the form, ThinkPHP6 also provides a wealth of data verification methods that can verify database data.
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; } }
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' ]; // ... }
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()); }
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!