定制 denarx/laravel-custom-validator 二次开发

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

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

denarx/laravel-custom-validator

Composer 安装命令:

composer require denarx/laravel-custom-validator

包简介

Extends default laravel validator and make rules more strict

README 文档

README

Default Illuminate validator has some bugs and features. This package make it more strict and additionally allows you easily to extend it, without creating custom Factory and ServiceProvider, what make possible to change behavior of build-in rules

Installation

composer require show4me/laravel-custom-validator

for Lumen: add to your bootstrap/app.php

$app->register(Denarx\LaravelCustomValidator\ValidationServiceProvider::class);

What fixed by default

  • integer - accept bool true as 1, now throw an error
  • not required integer - accept empty string and null, now throw an error
  • not required string - don't check min length, just skipped, now throw an error

Overwrite/extend in your application

You can simply extend for adding/changing rules by creating file /app/Validator/Validator.php

<?php

namespace App\Validator;

use Denarx\LaravelCustomValidator\Validator as CustomValidator;

class Validator extends CustomValidator
{
    public function validateNewRule($attribute, $value): bool
    {
        return $value==='test';
    }

    public function validateInteger($attribute, $value): bool
    {
        return !is_string($value) && is_numeric($value) && filter_var($value, FILTER_VALIDATE_INT) !== false;
    }
}

In this example the default rule "integer" overwrited for more strict behavior, numeric strings (like "1") will be rejected. And we added new rule with name "new_rule" that accepts only string 'test'. On reject, we'll get an error message: "Error while validation" For customise it we need to create file /resources/lang/en/validation.php

<?php

$defaultTranslation = include(base_path('/vendor/laravel/lumen-framework/resources/lang/en/validation.php'));
$customTranslation = [
    'new_rule' => 'The :attribute must be equal "test".',
];

return array_merge($defaultTranslation, $customTranslation);

And now we have message : "The title must be equal "test"."

You can use just an extent without changing default rules behavior by creating the same file /app/Validator/Validator.php but with extending DefaultValidator instead CustomValidator. In that case validator has default Illuminate behavior, but you can easily extend it still

<?php

namespace App\Validator;

use Illuminate\Validation\Validator as DefaultValidator;

class Validator extends DefaultValidator
{
    public function validateNewRule($attribute, $value): bool
    {
        return $value==='test';
    }
}

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固