定制 solbianca/validator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

solbianca/validator

Composer 安装命令:

composer require solbianca/validator

包简介

Easy to use, highly customisable PHP validator.

README 文档

README

This is an easy to use and customisable PHP validator.

Note: This package is under development and not recommended for production.

Build Status Scrutinizer Code Quality Maintainability

Installing

Install via composer

composer require solbianca/validator

or add to composer.json

{
    "require": {
        "solbianca/php-validator": "1.*"
    }
}

Basic usage

use SolBianca\Validator;

$validator = new Validator;

$validator->validate([
    'age' => ['value' => 20, 'rules' => ['required', 'int']],
    'name' => ['value' => 'John Doe', 'rules' => ['required']],
]);

if($validator->passed()) {
    echo 'Validation passed!';
} else {
    var_dump($validator->errors()->all();
}

Adding custom rules

Adding custom rules is simple. It can be any callable or object which implement SolBianca\Validator\Interfaces\RuleInterface

If the callable returns false, the rule fails.

$validator->addRule('sex', function ($value) {
    return in_array($value, ['male', 'female']);
})->addRuleMessage('sex', 'Field `{field}` must be male or female. Given value `{value}`.');

$validator->validate([
    'fruit' => ['value' => 'male', 'rules' => ['sex']],
]);
class SomeRule implements SolBianca\Validator\Interfaces\RuleInterface 
{
    // some code
}

// You can add as a string
$validator->addRule('sex', SomeRule::class);

// or as an object
$validator->addRule('sex', new SomeRule());

Rewrite rules

Validator have useful default rules as int, required and many more. You can rewrite any rule by your own.

$validator->addRule('int', function ($value) {
    return (is_int($value)) && $value > 0);
})->addRuleMessage('sex', 'Field `{field}` must be integer ang greater than zero.');

$validator->validate([
    'fruit' => ['value' => 'male', 'rules' => ['sex']],
]);

Adding custom error messages

You can add custom error messages for any rule

$validator->addRuleMessage('required', 'You better fill in the {field} field, or else.');

Adding rule messages in bulk

$v->addRuleMessages([
    'required' => 'You better fill in the {field} field, or else.',
    'int'      => 'The {field} needs to be an integer, but I found {value}.',
]);

Using Field Aliases

Field Aliases helps you format any error messages without showing weird form names or the need to create a custom error.

$validator->validate([
    'username_box' => ['value' => '', 'rules' => ['required'], 'alias' => 'Username']
]);

// Error output: "Field `Username` is required."

Rules

Array

If the value is an array.

$validator->validate([
    'some_input' => ['value' => [10, 20], 'rules' => ['array']],
]);

Between

Checks if the value is within the intervals defined. This check is inclusive, so 5 is between 5 and 10.

$validator->validate([
    'some_input' => ['value' => 5, 'rules' => ['between' => [5, 10]]],
]);

Bool

If the value is a boolean.

$validator->validate([
    'some_input' => ['value' => true, 'rules' => ['bool']],
]);

Email

If the value is a valid email.

$validator->validate([
    'some_input' => ['value' => 'mail@example.com', 'rules' => ['email']],
]);

Int

If the value is an integer, including numbers within strings. 1 and '1' are both classed as integers.

$validator->validate([
    'some_input' => ['value' => 42, 'rules' => ['int']],
]);

Ip

If the value is a valid IP address.

$validator->validate([
    'some_input' => ['value' => '127.0.0.1', 'rules' => ['ip']],
]);

Matches

Checks if one given input matches the other. For example, checking if password matches password_confirm.

$validator->validate([
    'some_input' => ['value' => 1, 'rules' => ['int', 'matches' => 'other_input']],
    'other_input' => ['value' => 1, 'rules' => ['int']]
]);

Max

Check if string length is less than or equal to given int. To check the size of a number, pass the optional number option.

$validator->validate([
    'some_input' => ['value' => 5, 'rules' => ['max' => 10]],
    'other_input' => ['value' => 0.5, 'rules' => ['max' => [1.0, 'number']]],
]);

Mix

Check if string length is greater than or equal to given int. To check the size of a number, pass the optional number option.

$validator->validate([
    'some_input' => ['value' => 5, 'rules' => ['min' => 1]],
    'other_input' => ['value' => 0.5, 'rules' => ['min' => [0.0, 'number']]],
]);

Number

If the value is a number, including numbers within strings.

Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part. Thus +0123.45e6 is a valid numeric value. Hexadecimal (e.g. 0xf4c3b00c), Binary (e.g. 0b10100111001), Octal (e.g. 0777) notation is allowed too but only without sign, decimal and exponential part.

$validator->validate([
    'some_input' => ['value' => '5', 'rules' => ['number']],
]);

Regex

If the given input has a match for the regular expression given.

$validator->validate([
    'some_input' => ['value' => 'bag', 'rules' => ['regex' => '/b[aeiou]g/']],
]);

Required

If the value is present.

$validator->validate([
    'some_input' => ['value' => true, 'rules' => ['required']],
]);

Url

If the value is formatted as a valid URL.

$validator->validate([
    'some_input' => ['value' => 'http://example.com', 'rules' => ['url']],
]);

solbianca/validator 适用场景与选型建议

solbianca/validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 12 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「validator」 「validation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 solbianca/validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 solbianca/validator 我们能提供哪些服务?
定制开发 / 二次开发

基于 solbianca/validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-12-09