定制 fyre/validation 二次开发

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

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

fyre/validation

Composer 安装命令:

composer require fyre/validation

包简介

A validation library.

README 文档

README

FyreValidation is a free, open-source validation library for PHP.

Table Of Contents

Installation

Using Composer

composer require fyre/validation

In PHP:

use Fyre\Validation\Validator;

Basic Usage

$validator = new Validator($container, $lang);

Autoloading

Any dependencies will be injected automatically when loading from the Container.

$validator = $container->use(Validator::class);

Methods

Add

Add a validation rule.

  • $field is a string representing the field name.
  • $rule is a Closure or a Rule representing the validation rule.
  • $options is an array containing options for the validation rule.
    • on is a string representing the type of validation the rule applies to, and will default to null.
    • message is a string representing the error message for the rule, and will default to null.
    • name is a string representing the name of the validation rule, and will default to null.
$validator->add($field, $rule, $options);

If using a Closure rule, the $rule should be expressed in the following format:

$rule = function(mixed $value, array $data, string $field): string|bool {
    // validation logic

    return true;
};

Any other arguments will be resolved from the Container.

Clear

Clear all rules from the Validator.

$validator->clear();

Get Field Rules

Get the rules for a field.

  • $field is a string representing the field name.
$rules = $validator->getFieldRules($field);

Remove

Remove a validation rule.

  • $field is a string representing the field name.
  • $name is a string representing the rule name.
$removed = $validator->remove($field, $name);

If the $name argument is omitted, all rules will be removed instead.

$validator->remove($field);

Validate

Perform validation and return any errors.

  • $data is an array containing the data to validate.
  • $type is a string representing the type of validation, and will default to null.
$errors = $validator->validate($data, $type);

Rules

use Fyre\Validation\Rule;

Alpha

Create an "alpha" Rule.

Rule::alpha();

Alpha Numeric

Create an "alpha-numeric" Rule.

Rule::alphaNumeric();

Ascii

Create an "ASCII" Rule.

Rule::ascii();

Between

Create a "between" Rule.

  • $min is a number representing the minimum value (inclusive).
  • $max is a number representing the maximum value (inclusive).
Rule::between($min, $max);

Boolean

Create a "boolean" Rule.

Rule::boolean();

Decimal

Create a "decimal" Rule.

Rule::decimal();

Date

Create a "date" Rule.

Rule::date();

DateTime

Create a "date/time" Rule.

Rule::dateTime();

Differs

Create a "differs" Rule.

  • $field is a string representing the other field to compare against.
Rule::differs($field);

Email

Create an "email" Rule.

Rule::email();

Empty

Create an "empty" Rule.

Rule::empty();

Equals

Create an "equals" Rule.

  • $value is the value to compare against.
Rule::equals($value);

Exact Length

Create an "exact length" Rule.

  • $length is a number representing the length.
Rule::exactLength($length);

Greater Than

Create a "greater than" Rule.

  • $min is the minimum value.
Rule::greaterThan($min);

Greater Than Or Equals

Create a "greater than or equals" Rule.

  • $min is the minimum value.
Rule::greaterThanOrEquals($min);

In

Create an "in" Rule.

  • $values is an array containing the values to compare against.
Rule::in($values);

Integer

Create an "integer" Rule.

Rule::integer();

Ip

Create an "IP" Rule.

Rule::ip();

Ipv4

Create an "IPv4" Rule.

Rule::ipv4();

Ipv6

Create an "IPv6" Rule.

Rule::ipv6();

Less Than

Create a "less than" Rule.

  • $max is the maximum value.
Rule::lessThan($max);

Less Than Or Equals

Create a "less than or equals" Rule.

  • $max is the maximum value.
Rule::lessThanOrEquals($max);

Matches

Create a "matches" Rule.

  • $field is a string representing the other field to compare against.
Rule::matches($field);

Max Length

Create a "maximum length" Rule.

  • $length is a number representing the maximum length.
Rule::maxLength($length);

Min Length

Create a "minimum length" Rule.

  • $length is a number representing the minimum length.
Rule::minLength($length);

Natural Number

Create a "natural number" Rule.

Rule::naturalNumber();

Not Empty

Create an "not empty" Rule.

Rule::notEmpty();

Regex

Create a "regular expression" Rule.

  • $regex is a string representing the regular expression.
Rule::regex($regex);

Required

Create a "required" Rule.

Rule::required();

Require Presence

Create a "require presence" Rule.

Rule::requirePresence();

Time

Create a "time" Rule.

Rule::time();

Url

Create a "URL" Rule.

Rule::url();

Error Messages

Custom error messages can be used by supplying the message property of the $options array to the Validator add method.

$validator->add('field', Rule::required(), [
    'message' => 'The field is required.'
]);

Alternatively, for custom validation callbacks, a string can be returned and that will be used as the error messages.

$validator->add('field', function(mixed $value): bool|string {
    if ($value) {
        return true;
    }

    return 'The field is required.';
});

If a custom error message is not supplied, the rule name will be used to retrieve a Lang value. The field placeholder can be used for the field name, and any arguments supplied to the rule will be available as numeric placeholders.

// language/en/Validation.php

return [
    'required' => 'The {field} is required.'
];

If no error message is available, the error message will simply be set to "invalid".

fyre/validation 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-01-15