Home

Awesome

<h1 align="center">Validator</h1> <p align="center">Lightweight Validation Like Laravel</p> <p align="center"> <a href="https://travis-ci.org/RunnerLee/validator"><img src="https://travis-ci.org/RunnerLee/validator.svg?branch=master" /></a> <a href="https://scrutinizer-ci.com/g/RunnerLee/validator/?branch=master"><img src="https://scrutinizer-ci.com/g/RunnerLee/validator/badges/coverage.png?b=master" title="Scrutinizer Code Coverage"></a> <a href="https://scrutinizer-ci.com/g/RunnerLee/validator/?branch=master"><img src="https://scrutinizer-ci.com/g/RunnerLee/validator/badges/quality-score.png?b=master" title="Scrutinizer Code Quality"></a> <a href="https://styleci.io/repos/82630238"><img src="https://styleci.io/repos/82630238/shield?branch=master" alt="StyleCI"></a> <a href="https://github.com/RunnerLee/validator"><img src="https://poser.pugx.org/runner/validator/v/stable" /></a> <a href="http://www.php.net/"><img src="https://img.shields.io/badge/php-%3E%3D5.6-8892BF.svg" /></a> <a href="https://github.com/RunnerLee/validator"><img src="https://poser.pugx.org/runner/validator/license" /></a> </p>

使用

<?php
use Runner\Validator\Validator;

// 增加规则扩展
Validator::addExtension(
    'channel',
    function ($field, $value, array $parameters = []) {
        return false !== array_search($value, ['google', 'bing']);
    },
    false
);

$data = [
    'blog' => 'https://github.com/RunnerLee',
];
$rule = [
    'blog' => 'required|url',
];
$validator = new Validator($data, $rule);

var_dump($validator->validate()); // output: true

// 获取错误消息
$validator->messages();

内置验证规则

Note

关于空字符串

如果传入一个数组:

$data = [
    'foo' => '',
];

同时规则设置为:

$rules= [
    'foo' => 'string',
];

此时校验结果会是通过的. 也就是说 validator 不会把 '' 当作 null 处理.

参考

https://github.com/vlucas/valitron

https://github.com/illuminate/validation