denarx/laravel-custom-validator 问题修复 & 功能扩展

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

邮箱: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
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

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