Master Data Validation in ThinkPHP with ThinkValidate – Features, Installation & Advanced Usage
This guide introduces ThinkValidate, a PHP data validation library for ThinkPHP, covering its main features, Composer installation, method‑based rule definitions, rule aliases, array and multi‑dimensional validation, rule sets, and flexible validation groups with code examples.
ThinkValidate is a PHP data validation library maintained independently from the core component of ThinkPHP . It is known for functionality, performance, and a pleasant developer experience.
Main Features
Implemented with PHP 8 and strong typing.
Rich built‑in validation rules.
Supports validator class, array, and chainable method definitions.
Supports validation scenes and groups.
Supports independent data validation.
Supports enum validation.
Supports batch validation.
Supports throwing exceptions.
Installation
Install via Composer: composer require topthink/think-validate If your PHP version is lower than 8.0, version 2.0 will be installed automatically.
Method Definition
When property‑based definitions are insufficient, you can define validation rules through methods.
namespace app\validate;
use think\Validate;
class User extends Validate {
protected function rules() {
return [
'name' => 'require|max:25',
'age' => 'number|between:1,120',
'email' => 'email',
];
}
}Returning an array from rules disables rules defined via the rule property; using the object‑style rules merges with rule definitions, with later definitions overriding earlier ones for the same field.
namespace app\validate;
use think\Validate;
class User extends Validate {
protected function rules() {
return $this
->rule('name','require|max:25',[ 'require' => '名称必须', 'max' => '名称最多不能超过25个字符' ])
->rule('age','number|between:1,120',[ 'number' => '年龄必须是数字', 'between' => '年龄只能在1-120之间' ])
->rule('email','email','邮箱格式错误');
}
}Rule Aliases
You can define aliases for common rules to reuse them.
namespace app\validate;
use think\Validate;
class User extends Validate {
protected $alias = [
'name' => 'require|alphaNum|max:25',
'age' => 'number|between:1,120',
];
protected $rule = [
// use rule alias
'name' => 'name',
'age' => 'age',
'email' => 'email',
];
}Aliases may refer to any valid rule definition, whether a string or an array.
Array Validation
The library supports validation of array elements.
namespace app\validate;
use think\Validate;
class User extends Validate {
protected $rule = [
'name' => 'require|max:25',
'age' => 'number|between:1,120',
'info.email' => 'email',
'info.score' => 'number',
];
}Multi‑dimensional arrays are also supported:
namespace app\validate;
use think\Validate;
class User extends Validate {
protected $rule = [
'name' => 'require|max:25',
'age' => 'number|between:1,120',
// info is a two‑dimensional array
'info.*.email' => 'email',
'info.*.score' => 'number',
];
}Rule Set
Using a rule set simplifies array validation definitions.
namespace app\validate;
use think\Validate;
class Good extends Validate {
protected function rules() {
return $this->ruleSet('pay', [
'title' => 'require',
'price' => 'require|integer',
]);
}
}Equivalent explicit definition:
namespace app\validate;
use think\Validate;
class Good extends Validate {
protected function rules() {
return [
'pay.*.title' => 'require',
'pay.*.price' => 'require|integer',
];
}
}Nested rule sets are also possible:
namespace app\validate;
use think\Validate;
class Good extends Validate {
protected function rules() {
return $this->ruleSet('pay', [
'total' => 'require|integer',
'item.*' => $this->rules([
'title' => 'require',
'price' => 'require|integer',
]),
]);
}
}Validation Groups
Rules can be grouped so that each group operates independently while sharing aliases and error messages.
namespace app\validate;
use think\Validate;
class Project extends Validate {
protected $message = [
'name.require' => '名称必须',
'name.max' => '名称最多不能超过25个字符',
'age.number' => '年龄必须是数字',
'age.between' => '年龄只能在1-120之间',
'email' => '邮箱格式错误',
];
protected $group = [
'user' => [
'name' => 'require|max:25',
'age' => 'number|between:1,120',
'email' => 'email',
],
];
}Validate a specific group:
$data = [
'name' => 'thinkphp',
'age' => 10,
'email' => '[email protected]',
];
try {
validate(app\validate\Project::class)
->check($data, 'user');
} catch (ValidateException $e) {
// validation failed, output error message
dump($e->getError());
}Groups can also be defined via methods named rules{GroupName} and invoked with a Validate object:
namespace app\validate;
use think\Validate;
class Project extends Validate {
protected $rule = [
'name' => 'require|max:25',
'age' => 'number|between:1,120',
'email' => 'email',
];
protected $message = [
'name.require' => '名称必须',
'name.max' => '名称最多不能超过25个字符',
'age.number' => '年龄必须是数字',
'age.between' => '年龄只能在1-120之间',
'email' => '邮箱格式错误',
];
public function rulesUser() {
return [
'name' => 'require|max:25',
'age' => 'number|between:1,120',
'email' => 'email',
];
}
}When using method‑defined groups, the validate parameter must be a Validate object.
namespace app\validate;
use think\Validate;
class Project extends Validate {
protected $rule = [
'name' => 'require|max:25',
'age' => 'number|between:1,120',
'email' => 'email',
];
protected $message = [
'name.require' => '名称必须',
'name.max' => '名称最多不能超过25个字符',
'age.number' => '年龄必须是数字',
'age.between' => '年龄只能在1-120之间',
'email' => '邮箱格式错误',
];
public function rulesUser(Validate $validate) {
return $validate->rule([
'name' => 'require|max:25',
'age' => 'number|between:1,120',
'email' => 'email',
]);
}
}Groups can be combined with scenes or the only method for greater flexibility.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
