368 Star 1.4K Fork 531

微擎 / 表单验证

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or Download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README-EN.md

Micro Engine Form Validate

Stat Forks

License PHP Version Support FOSSA Status Tests Download

中文 | English

Introduction

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

Catalog

Install

using Composer

composer require w7/engine-validate

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',
    ])->check($data);
} 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',
    ];
}

:::tip

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();
$validate->check($data);

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

$rule

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
$v->setRules([
    'user' => 'required'
]);

$message

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
$v->setMessages([
    'user.required' => 'pass is required'
]);

$scene

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
$v->setScene([
    'login' => ['user', 'pass']
]);

$event

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

protected $handler = [
    CheckSiteStatus::class
];

$default

Defining the default value of a field

protected $default = [
    'name' => 'Emma'
];

For more information about default values, please see the section Default Values.

$filter

For processing data after data validation

protected $filter = [
    'name' => 'trim'
];

For more information about filters, please see the Filter section.

$customAttributes

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'
];

$ruleMessage

Error messages for class method rules

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

Click to view example

$filled

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, present,required, required_if, required_unless, required_with,required_with_all, required_without, required_without_all Rule
  • Validation rules exist extendImplicit Defined rules
  • Validation rules exist extendImplicitRule Defined rules
  • The validation rules implement the Itwmw\Validation\Support\Interfaces\ImplicitRule Marker Interface
protected bool $filled = true;

$regex

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

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

Repository Comments ( 8 )

Sign in to post a comment

About

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

Releases (3)

All

表单验证

Contributors

All

Activities

Load More
can not load any more
PHP
1
https://git.oschina.net/we7coreteam/w7-engine-validate.git
git@git.oschina.net:we7coreteam/w7-engine-validate.git
we7coreteam
w7-engine-validate
表单验证
4.x

Search