460 Star 1.7K Fork 651

虞灪 / 表单验证

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README-EN.md 9.00 KB
一键复制 编辑 原始数据 按行查看 历史
虞灪 提交于 2023-02-02 18:36 . [U] 修改项目说明

Micro Engine Form Validate

Star Fork

Latest Stable Version Total Downloads Tests License PHP Version Require

中文 | 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+$/'
];

$group

Define validation rule groups

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

protected $rule = [
    'username' => 'required|username'
];
PHP
1
https://gitee.com/yepyuyu/wm-validate.git
git@gitee.com:yepyuyu/wm-validate.git
yepyuyu
wm-validate
表单验证
4.x

搜索帮助