2017 © Pedro Peláez
 

library validator

yeah, another params validate component for PHP applications.

image

furthestworld/validator

yeah, another params validate component for PHP applications.

  • Thursday, June 8, 2017
  • by xuanskyer
  • Repository
  • 1 Watchers
  • 1 Stars
  • 284 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 13 Versions
  • 21 % Grown

The README.md

validator

yeah, another params validate component for PHP applications., (*1)

嗯,又一个PHP应用参数验证组件, (*2)

安装

  • 在项目的composer.json文件中的require项中添加:
"furthestworld/validator": "~1.0"

并更新composer依赖:composer update, (*3)

  • 在需要使用Validator服务的地方添加:
require_once __ROOT__ . '/vendor/autoload.php';
use FurthestWorld\Validator\Src\Validator;

食用方法

//扩展验证规则实例
Validator::extend('extend_test', new TestExtendRules());

Validator::formatParams(
    $params,
    [
        'domain'    => ['format_rule' => 'strtoupper', 'default_value' => ''],
        'member_id' => ['format_rule' => 'formatExtendMemberId:domain']
    ]
);
Validator::validateParams(
    $params,
    [
        'domain'    => ['check_rule' => 'number#numberGt0|string#string:10,500'],
        'member_id' => ['check_rule' => 'extendEq:20#number'],
    ]
);

if (!Validator::pass()) {
    //验证未通过
    var_dump(Validator::getErrors());
} else {
    //验证通过
}

语法说明

参数格式化

  • format_rule 规则: 格式化方法(PHP函数或自定义函数):格式化参数(若为空则默认为当前字段的值), (*4)

  • default_value 参数默认值设置, (*5)

  • force_value 强制重置参数, (*6)

参数验证

  • check_rule

分隔符 |或验证(满足其中的至少一项验证), (*7)

分隔符 #与验证(满足其中所有的验证项), (*8)

分隔符 : :方法和参数分隔符, (*9)

分隔符 , :多个参数分隔符, (*10)

如上面的规则:'number#numberGt0|string#string:10,500' 解析成PHP代码逻辑相当于:, (*11)

   if((number && numberGt0) || (string && string:10,500)){
       ...
   }

为了避免方法名和PHP关键字冲突,对应规则中的方法在解析成方法名时会自动加上前缀 check 比如上面的numbernumberGt0string 验证方法对应的方法分别为: checkNumbercheckNumberGt0checkString, (*12)

规则实例扩展

format_rulecheck_rule 除了使用组件自带的格式化和验证方法,也支持自定义方法。 只需要把自定义的规则实例注册到组件的扩展规则中就可以了,如:, (*13)

Validator::extend('extend_test', new TestExtendRules());

enjoy~ :)

The Versions

08/06 2017

dev-master

9999999-dev

yeah, another params validate component for PHP applications.

  Sources   Download

MIT

The Requires

  • php >=5.5.19

 

by Avatar xuanskyer

php validator

08/06 2017

v1.0.10

1.0.10.0

yeah, another params validate component for PHP applications.

  Sources   Download

MIT

The Requires

  • php >=5.5.19

 

by Avatar xuanskyer

php validator

26/12 2016

v1.0.9

1.0.9.0

yeah, another params validate component for PHP applications.

  Sources   Download

MIT

The Requires

  • php >=5.6.19

 

by Avatar xuanskyer

php validator

26/12 2016

v1.0.8

1.0.8.0

yeah, another params validate component for PHP applications.

  Sources   Download

MIT

The Requires

  • php >=5.6.19

 

by Avatar xuanskyer

php validator

26/12 2016

v1.0.7

1.0.7.0

yeah, another params validate component for PHP applications.

  Sources   Download

MIT

The Requires

  • php >=5.6.19

 

by Avatar xuanskyer

php validator

26/12 2016

v1.0.6

1.0.6.0

yeah, another params validate component for PHP applications.

  Sources   Download

MIT

The Requires

  • php >=5.6.19

 

by Avatar xuanskyer

php validator

26/12 2016

v1.0.5

1.0.5.0

yeah, another params validate component for PHP applications.

  Sources   Download

MIT

The Requires

  • php >=5.6.19

 

by Avatar xuanskyer

php validator

23/12 2016

v1.0.4

1.0.4.0

yeah, another params validate component for PHP applications.

  Sources   Download

MIT

The Requires

  • php >=5.6.19

 

by Avatar xuanskyer

php validator

23/12 2016

dev-dev

dev-dev

yeah, another params validate component for PHP applications.

  Sources   Download

MIT

The Requires

  • php >=5.6.19

 

by Avatar xuanskyer

php validator

23/12 2016

v1.0.3

1.0.3.0

yeah, another params validate component for PHP applications.

  Sources   Download

MIT

The Requires

  • php >=5.6.19

 

by Avatar xuanskyer

php validator

23/12 2016

v1.0.2

1.0.2.0

yeah, another params validate component for PHP applications.

  Sources   Download

MIT

The Requires

  • php >=5.6.19

 

by Avatar xuanskyer

php validator

23/12 2016

v1.0.1

1.0.1.0

yeah, another params validate component for PHP applications.

  Sources   Download

MIT

The Requires

  • php >=5.6.19

 

by Avatar xuanskyer

php validator

23/12 2016

v1.0.0

1.0.0.0

yeah, another params validate component for PHP applications.

  Sources   Download

MIT

The Requires

  • php >=5.6.19

 

by Avatar xuanskyer

php validator