430 Star 1.6K Fork 620

微擎 / 表单验证

Create your Gitee Account
Explore and code with more than 8 million developers,Free private repositories !:)
Sign up
Clone or Download
Sync branch
Notice: Creating folder will generate an empty file .keep, because not support in Git

Micro Engine Form Validate

Star Fork

Latest Stable Version Total Downloads Tests License PHP Version Require

中文 | English


An extension to make your form validation easier, faster and more secure for all your validation needs.



using Composer

composer require w7/engine-validate

View the complete document here.

Simple Validate

Support for simply defining a validator and performing validation:

try {
    $data = Validate::make([
        'user' => 'required|email',
        'pass' => 'required|lengthBetween:6,16',
    ], [
        'user.required'      => 'Please enter your username',
        'user.email'         => 'User name format error',
        'pass.required'      => 'Please enter your password',
        'pass.lengthBetween' => 'Password length is 6~16 bits',
} catch (ValidateException $e) {
    echo $e->getMessage();

If the validation passes, all values that passed the validation are returned, if not, a W7\Validate\Exception\ValidateException exception is thrown

Validator Definition

To define the validator class for a specific validation scenario or data form, we need to inherit the W7\Validate\Validate class, Then instantiate and call the check method of the validation class directly to complete the validation, an example of which is as follows.

Define a LoginValidate validator class for login validation.

class LoginValidate extends Validate
    protected $rule = [
        'user' => 'required|email',
        'pass' => 'required|digits_between:6,16',
    protected $message = [
        'user.required'       => 'Please enter your username',
        'user.email'          => 'Incorrect username format',
        'pass.required'       => 'Please enter your password',
        'pass.digits_between' => 'Password length of 6 to 16 digits',


Error messages defined by class attributes take precedence over the default responses in custom rules and over the errors returned by custom rule methods.

Data Validate

$data = [
    'user' => '123@qq.com',
    'pass' => ''
$validate = new LoginValidate();

This throws a W7\Validate\Exception\ValidateException exception with the message Please enter your password

$data = [
    'user' => '123@qq.com',
    'pass' => '123456'
$validate = new LoginValidate();
$data = $validate->check($data);

Validates successfully and returns the validated value, which is an array type

Validate Arrays

It is not difficult to validate the form input as an array of fields. You can use the "dot" method to validate properties in an array. For example, if the incoming HTTP request contains the search[keyword] field, it can be validated as follows.

protected $rule = [
    'search.order'   => 'numeric|between:1,2',
    'search.keyword' => 'chsAlphaNum',
    'search.recycle' => 'boolean',

You can also validate each element in an array. For example, to validate that each id in a given array input field is unique, you can do this.

protected $rule = [
    'search.*.id' => 'numeric|unique:account'

The definition of an error message for an array rule is the same

protected $message = [
    'search.order.numeric'       => 'order parameter error',
    'search.order.between'       => 'order parameter error',
    'search.keyword.chsAlphaNum' => 'Keywords can only contain Chinese, letters, numbers',
    'search.recycle.boolean'     => 'Parameter error:recycle',

Validator Class Attributes


The validation rules of the user-defined validator can also be set via the setRules method, This method will superimpose the data.If the parameter is null then it is clear all rules.

// Definition in class
protected $rule = [
    'user' => 'required'

// Definition of usage methods
    'user' => 'required'


User-defined validator error messages can also be set via the setMessages method, This method will superimpose the data,If the parameter is null then it is clear all error messages.

// Definition in class
protected $message = [
    'user.required' => 'user is required'

// Definition of usage methods
    'user.required' => 'pass is required'

For more usage examples, please refer to the Message section.


Define the data of the validation scenario, which is used to specify the validation fields corresponding to the validation scenario, etc. See the section on validate scene for detailed usage.

The same can be done with the setScene method. This method will superimpose the data, If the parameter is null, then all validate scene are cleared.

// Definition in class
protected $scene = [
    'login' => ['user', 'pass']

// Definition of usage methods
    'login' => ['user', 'pass']


Define global events under this validator, see the section Events for detailed usage.

protected $handler = [


Defines a preprocessing handler for a field.

protected $preprocessor = [
    'name' => ['Default Name', ProcessorExecCond::WHEN_EMPTY]

For more details about preprocessing handlers, please refer to the Processor section.


Defines a postprocessing handler for a field.

protected $postprocessor = [
    'name' => ['trim', ProcessorExecCond::WHEN_NOT_EMPTY, ProcessorParams::Value]

For more details about postprocessing handlers, please refer to the Processor section.


Define the name of the validation field, also can be set by setCustomAttributes method, this method will superimpose the data, if the parameter is null then it is clear all field names. the :attribute in the error message will be replaced with the value corresponding to the following

protected $customAttributes = [
    'user' => 'Account',
    'pass' => 'Password'


The error message for failed validation of a custom method in the current class.

 protected $ruleMessage = [
    'The value of :attribute can only have Chinese'

Click to view example


All validated fields cannot be empty when they exist, if the value of this property is true, all rules will automatically add filled rules, the default is true

The filled rule is not automatically added when:

  • Validation rules exist in filled, nullable, accepted, declined, declined_if, present, required, required_if, required_unless, required_with,required_with_all, required_without, required_without_all Rule
  • Validation rules exist extendImplicit Defined rules
  • The validation rules implement the Itwmw\Validation\Support\Interfaces\ImplicitRule Marker Interface
protected bool $filled = true;


Predefined regular expression validation rules, see Regular Expression Rule for details

protected $regex = [
	'number' => '/^\d+$/'


Define validation rule groups

protected $group = [
    'username' => 'alpha_num|min:5|max:10'

protected $rule = [
    'username' => 'required|username'

Can also use array format:

protected $group = [
    'username' => [

protected $rule = [
    'username' => 'required|username'


PHP验证解决方案,一个让你的表单验证更为方便,快捷,安全的扩展,满足你的一切验证需求。 expand collapse


No release





Load More
can not load any more