arifszn/laravel-advanced-validation
Composer 安装命令:
composer require arifszn/laravel-advanced-validation
包简介
Laravel advanced validation rules for real-life scenarios.
关键字:
README 文档
README
Laravel advanced validation rules for real-life scenarios.
Installation
Install via composer
composer require arifszn/laravel-advanced-validation
Laravel uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.
Translations
If you wish to edit the package translations, you can run the following command to publish them into your resources/lang folder
php artisan vendor:publish --provider="Arifszn\AdvancedValidation\ServiceProvider"
Custom Error Message
You can specify the error message on the fly when declaring the rules. Simple pass the error message parameter.
use Arifszn\AdvancedValidation\Rules\Username; public function rules() { return [ 'foo' => [new Username('Your custom error message')], ]; }
Available Validation Rules
AsciiBase64 ImageBase64 StringBICBtc AddressCredit CardData URIDivisible ByEthereum AddressFloat NumberHashImage URLJWTNamePhoneUsernameWithout Spaces
Ascii
The field under validation must contain ASCII chars only.
public Arifszn\AdvancedValidation\Rules\Ascii::__construct(string $errorMessage = null)
use Arifszn\AdvancedValidation\Rules\Ascii; public function rules() { return [ 'foo' => [new Ascii()], ]; }
Base64Image
The field under validation must be a Base64 encoded image.
public Arifszn\AdvancedValidation\Rules\Base64Image::__construct(string $errorMessage = null)
use Arifszn\AdvancedValidation\Rules\Base64Image; public function rules() { return [ 'avatar' => [new Base64Image()], ]; }
Base64String
The field under validation must be a Base64 encoded string.
public Arifszn\AdvancedValidation\Rules\Base64String::__construct(string $errorMessage = null)
use Arifszn\AdvancedValidation\Rules\Base64String; public function rules() { return [ 'foo' => [new Base64String()], ]; }
BIC
The field under validation must be a BIC(Business Identifier Code) or SWIFT code.
public Arifszn\AdvancedValidation\Rules\BIC::__construct(string $errorMessage = null)
use Arifszn\AdvancedValidation\Rules\BIC; public function rules() { return [ 'foo' => [new BIC()], ]; }
BtcAddress
The field under validation must be a valid BTC address.
public Arifszn\AdvancedValidation\Rules\BtcAddress::__construct(string $errorMessage = null)
use Arifszn\AdvancedValidation\Rules\BtcAddress; public function rules() { return [ 'foo' => [new BtcAddress()], ]; }
CreditCard
The field under validation must be a valid credit card number.
public Arifszn\AdvancedValidation\Rules\CreditCard::__construct(string $errorMessage = null)
use Arifszn\AdvancedValidation\Rules\CreditCard; public function rules() { return [ 'foo' => [new CreditCard()], ]; }
DataURI
The field under validation must have data uri format.
public Arifszn\AdvancedValidation\Rules\DataURI::__construct(string $errorMessage = null)
use Arifszn\AdvancedValidation\Rules\DataURI; public function rules() { return [ 'foo' => [new DataURI()], ]; }
DivisibleBy
The field under validation must be divisible by the given number.
public Arifszn\AdvancedValidation\Rules\DivisibleBy::__construct(int $number, string $errorMessage = null)
use Arifszn\AdvancedValidation\Rules\DivisibleBy; public function rules() { return [ 'foo' => [new DivisibleBy(2)], ]; }
EthereumAddress
The field under validation must be an Ethereum address. Does not validate address checksums.
public Arifszn\AdvancedValidation\Rules\EthereumAddress::__construct(string $errorMessage = null)
use Arifszn\AdvancedValidation\Rules\EthereumAddress; public function rules() { return [ 'foo' => [new EthereumAddress()], ]; }
FloatNumber
The field under validation must be a float number.
public Arifszn\AdvancedValidation\Rules\FloatNumber::__construct(string $errorMessage = null)
use Arifszn\AdvancedValidation\Rules\FloatNumber; public function rules() { return [ 'foo' => [new FloatNumber()], ]; }
Hash
The field under validation must be a hash of type algorithm.
Algorithm is one of 'md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b'.
public Arifszn\AdvancedValidation\Rules\Hash::__construct(string $algorithm, string $errorMessage = null)
use Arifszn\AdvancedValidation\Rules\Hash; public function rules() { return [ 'foo' => [new Hash('md4')], ]; }
ImageURL
The field under validation must be a valid image URL.
✓ https://www.php.net/images/logos/php-logo.png
✕ https://imaginarysite123.com/invalid.png
public Arifszn\AdvancedValidation\Rules\ImageURL::__construct(string $errorMessage = null)
use Arifszn\AdvancedValidation\Rules\ImageURL; public function rules() { return [ 'avatar' => [new ImageURL()], ]; }
JWT
The field under validation must have a valid format of JWT (JSON Web Token).
public Arifszn\AdvancedValidation\Rules\Jwt::__construct(string $errorMessage = null)
use Arifszn\AdvancedValidation\Rules\Jwt; public function rules() { return [ 'foo' => [new Jwt()], ]; }
Name
The field under validation must be a valid name.
- no emoji
- no number (if
$allowNumberflag is true, it will accept numbers, default is false) - special characters are allowed (restricting special characters will cause false-negative for names like
Martin Luther King, Jr.or李小龍)
public Arifszn\AdvancedValidation\Rules\Name::__construct(bool $allowNumber = false, string $errorMessage = null)
use Arifszn\AdvancedValidation\Rules\Name; public function rules() { return [ 'name' => [new Name()], ]; }
Phone
The field under validation must be a valid phone number.
✓ +x-xxx-xxx-xxxx
✓ +xxxxxxxxxxx
✓ (xxx) xxx-xxxx
✓ xxxxxxxxxx
public Arifszn\AdvancedValidation\Rules\Phone::__construct(string $errorMessage = null)
use Arifszn\AdvancedValidation\Rules\Phone; public function rules() { return [ 'foo' => [new Phone()], ]; }
Username
The field under validation must be a valid username.
- starts with a letter (alpha)
- only alpha-numeric (a-z, A-Z, 0-9), underscore, minus and dot
- multiple underscores, minus and are not allowed (-- or __ or ..)
- underscores, minus and dot are not allowed at the beginning or end
public Arifszn\AdvancedValidation\Rules\Username::__construct(string $errorMessage = null)
use Arifszn\AdvancedValidation\Rules\Username; public function rules() { return [ 'username' => [new Username()], ]; }
WithoutSpaces
The field under validation must not contain spaces.
public Arifszn\AdvancedValidation\Rules\WithoutSpaces::__construct(string $errorMessage = null)
use Arifszn\AdvancedValidation\Rules\WithoutSpaces; public function rules() { return [ 'foo' => [new WithoutSpaces()], ]; }
Tips
If you want to use the rules as strings and use them globally e.g. 'foo' => ['phone'], you can do so by adding them to the boot method of your project's AppServiceProvider.
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Validator; use Arifszn\AdvancedValidation\Rules\Phone; class AppServiceProvider extends ServiceProvider { public function boot() { Validator::extend( 'phone', 'Arifszn\AdvancedValidation\Rules\Phone@passes', (new Phone())->message() ); } }
Testing
composer test
Contributing
Any contributors who want to make this project better can make contributions, which will be greatly appreciated. To contribute, clone this repo locally and commit your code to a new branch. Feel free to create an issue or make a pull request.
Credits
Support
License
This package is licensed under the MIT License.
arifszn/laravel-advanced-validation 适用场景与选型建议
arifszn/laravel-advanced-validation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 38 次下载、GitHub Stars 达 15, 最近一次更新时间为 2022 年 02 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validator」 「validation」 「laravel」 「laravel-package」 「validation-library」 「laravel-framework」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 arifszn/laravel-advanced-validation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 arifszn/laravel-advanced-validation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 arifszn/laravel-advanced-validation 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Runn Me! Validation and Sanitization Library
Extension for Opis JSON Schema
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
A Laravel validator for delimiter-separated list of emails.
A CakePHP behavior to validate foreign keys
统计信息
- 总下载量: 38
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-02-11