f9webltd/laravel-validation-rules
Composer 安装命令:
composer require f9webltd/laravel-validation-rules
包简介
A collection of useful Laravel validation rules
关键字:
README 文档
README
Useful Laravel Validation Rules
A collection of useful Laravel validation rules.
Requirements
PHP >= 7.2, Laravel >=5.8 | 6.x | 7.x | 8.x.
Installation
composer require f9webltd/laravel-validation-rules
To publish the package validation message translations:
php artisan vendor:publish --provider="F9Web\ValidationRules\ValidationRulesServiceProvider"
Published translations are available at resources/lang/vendor/f9web-validation-rules/messages.php.
Usage
As discussed in the official Laravel documentation, import the required rule whenever required:
use F9Web\ValidationRules\Rules\TitleCase; // ... $request->validate([ 'team' => ['required', new TitleCase()], ]);
Alternatively use the rule directly with a Laravel form request object
Available rules
Base64EncodedStringCoordinateDomainRestrictedEmailExcludesHtmlHexColourCodeHonorificIncludesHtmlNoWhitespaceNumberParityStringContainsStrongPasswordTitleCaseUKMobilePhoneUppercase
Base64EncodedString
Ensure the passed attribute is a valid base 64 encoded string.
Coordinate
Ensure the passed attribute is a valid comma separated Latitude and Longitude string. For example: 51.507877,-0.087732.
DomainRestrictedEmail
Ensure the passed email in question is part of the provided whitelist of domains.
For instance, to ensure the given email domain is f9web.co.uk or laravel.com:
use F9Web\ValidationRules\Rules\DomainRestrictedEmail; // ... $request->validate([ 'email' => [ 'required', (new DomainRestrictedEmail())->validDomains([ 'f9web.co.uk', 'laravel.com', ]), ], ]);
The validation message will include the list of whitelisted domains based upon the provided configuration.
ExcludesHtml
Ensure the passed attribute does not contain HTML.
HexColourCode
Ensure the passed attribute is a valid hex colour code (three of six characters in length), optionally validating the presence of the # prefix.
Minimum usage example to validate a short length code with the prefix i.e. #fff:
use F9Web\ValidationRules\Rules\HexColourCode; (new HexColourCode());
Extended usage example to validate a long length code , omitting prefix i.e. cc0000:
use F9Web\ValidationRules\Rules\HexColourCode; (new HexColourCode())->withoutPrefix()->longFormat();
Honorific
Ensure the passed attribute is a valid honorific, omitting appended dots. The list of valid honorifics is available here.
IncludesHtml
Ensure the passed attribute contains HTML.
NoWhitespace
Ensure the passed attribute contains no whitespace.
NumberParity
Validate the number parity.
An odd number:
use F9Web\ValidationRules\Rules\NumberParity; // ... $request->validate([ 'amount' => [ 'required', (new NumberParity())->odd(), ], ]);
An even number:
use F9Web\ValidationRules\Rules\NumberParity; // ... $request->validate([ 'amount' => [ 'required', (new NumberParity())->even(), ], ]);
StringContains
Ensure the given attribute contains the provided strings.
Minimum usage example to ensure the attribute in question contains the string php or laravel:
use F9Web\ValidationRules\Rules\StringContains; // ... $request->validate([ 'description' => [ 'required', (new StringContains())->phrases([ 'laravel', 'php', ]), ], ]);
Optionally force the string to contain all provided phrases:
use F9Web\ValidationRules\Rules\StringContains; // ... $request->validate([ 'description' => [ 'required', (new StringContains())->phrases([ 'laravel', 'php', ])->strictly(), ], ]);
The validation message will include the list phrases based upon the provided configuration.
StrongPassword
Ensure the given attribute matches the provided conditions.
Minimum usage example to ensure the attribute:
- is a minimum of eight characters in length
- contains upper and lowercase characters
- contains at least one number
use F9Web\ValidationRules\Rules\StrongPassword; // ... $request->validate([ 'password' => [ 'required', (new StrongPassword()), ], ]);
Additional methods are available.
use F9Web\ValidationRules\Rules\StrongPassword; // ... $request->validate([ 'password' => [ 'required', (new StrongPassword()) ->forceUppercaseCharacters() ->forceLowercaseCharacters(false) ->forceNumbers() ->forceSpecialCharacters() // ->withSpecialCharacters('£$*%^'), ], ]);
The default special characters are !@#$%^&*()\-_=+{};:,<."£~?|>. Optionally the withSpecialCharacters() method can be used to define a custom list.
TitleCase
Ensure the provided attribute is title case.
UKMobilePhone
Ensure the provided attribute is a valid UK mobile telephone number.
Uppercase
Ensure the provided attribute is entirely uppercase.
Contribution
Any ideas are welcome. Feel free to submit any issues or pull requests.
Testing
composer test
Security
If you discover any security related issues, please email rob@f9web.co.uk instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
f9webltd/laravel-validation-rules 适用场景与选型建议
f9webltd/laravel-validation-rules 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 251 次下载、GitHub Stars 达 15, 最近一次更新时间为 2020 年 06 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「laravel validation」 「laravel validation rules」 「custom laravel validation」 「laravel request validation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 f9webltd/laravel-validation-rules 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 f9webltd/laravel-validation-rules 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 f9webltd/laravel-validation-rules 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Magento 2 module which adds support for Speculation Rules API for instant page loads.
The coding standard applying to all Youdot PHP projects, based on Doctrine set of PHPCS rules, with additional checks.
Business rules engine tools.
A set of Laravel validation rules
Adds request-parameter validation to the SLIM 3.x PHP framework
The last validation library you will ever need!
统计信息
- 总下载量: 251
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-06-04