hasan-22/form-validator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

hasan-22/form-validator

Composer 安装命令:

composer require hasan-22/form-validator

包简介

Form data validation

README 文档

README

Form data validator

install

composer require hasan-22/form-validator

usage

require_once __DIR__ . '/vendor/autoload.php';

$data = ['name'=>'armia','age'=>''];
$result = \Validation\Validator::validate(
    [
        'name'=>[
            'rules'=>['Required','Max:20'],
        ],
        'age'=>[
            'rules'=>['Required', 'Numeric'],
        ]
    ],$data);
    
print_r($result);

// Output: 
// [
//     [0] => [
//         [age] => The field `age` is required
//     ],
//     [1] => [
//         [age] => The field `age` must be numeric
//     ]
// ]

// If all validation passes, you get an empty array

with custom message

require_once __DIR__ . '/vendor/autoload.php';

$data = ['name'=>'armia','age'=>''];
$result = \Validation\Validator::validate(
    [
        'name'=>[
            'rules'=>['Required','Max:20'],
            'messages'=>['custom required msg']
        ],
        'age'=>[
            'rules'=>['Required', 'Numeric'],
            'messages'=>['custom required msg','custom numeric msg']
        ]
    ],$data);
    
// Output: 
// [
//     [0] => [
//         [age] => custom required msg
//     ],
//     [1] => [
//         [age] => custom numeric msg
//     ]
// ]


// If all validation passes, you get an empty array
 

If you want to add custom validation, do so

    class Email implements \Validation\ValidationInterface{
    
        private array $formData;

        private array $errorMessage;
    
        private string $field;
    
        private string $additional;
    
        public function validate(){
            if(!filter_var($this->formData[$this->field], FILTER_VALIDATE_EMAIL)){
                return $this->errorMessage;
            }
        }
        
         public function formData(array $formData){
            $this->formData = $formData;
        }
        
        public function message(string $errorMessage){
            $this->errorMessage = [$this->field => empty($errorMessage) ? "The `{$this->field}` field is not an email" : $errorMessage];
        }
    
        public function field(string $field){
            $this->field = $field;
        }
        
        public function additionalData(string $additional)
        {
            $this->additional = $additional;
        }
    } 
    

Now you can use the Email class like this

require_once __DIR__ . '/vendor/autoload.php';
require_once 'Email.php';

$formData = ['email'=>'armia@gmail.com'];
$result = \Validation\Validator::validate(
    [
        'email'=>[
            'rules'=>['Required','Email']
        ],
    ],$data);

//Output: 
//[] 
//If all validation passes, you get an empty array

Regex:pattern

The field under validation must match the given regular expression.

$data = ['password'=>'123456','age'=>''];
$result = \Validation\Validator::validate(
    [
        'password'=>[
            'rules'=>['Regex:/[1-6]/']
        ]
    ],$data);

Available validations

Validtion Description Example
Required Checks that the field is not empty, these items are considered empty [ '', 0, null ]
Between Checks if the field is between two values Between:4,10
Email Checks if the field is email
In Checks if the field has a series of values or not In:admin,customer,vendor
Max Checks the field must be less than or equal to a value Max:20
Min Checks that the field must be greater than or equal to a value Min:7
Numeric Checks that the field must be a number
Str Checks that the field must be a string
Regex The field under validation must match the given regular expression Regex:/pattern/

hasan-22/form-validator 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-01-21