A collection of useful Laravel validation rules
f9webltd/laravel-validation-rules is a Laravel package for a collection of useful laravel validation rules.
It currently has 15 GitHub stars and 251 downloads on Packagist (latest version 1.1.1).
Install it with composer require f9webltd/laravel-validation-rules.
Discover more Laravel packages by f9webltd
or browse all Laravel packages to compare alternatives.
Last updated
A collection of useful Laravel validation rules.
PHP >= 7.2, Laravel >=5.8 | 6.x | 7.x | 8.x.
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.
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
Base64EncodedStringCoordinateDomainRestrictedEmailExcludesHtmlHexColourCodeHonorificIncludesHtmlNoWhitespaceNumberParityStringContainsStrongPasswordTitleCaseUKMobilePhoneUppercaseBase64EncodedStringEnsure the passed attribute is a valid base 64 encoded string.
CoordinateEnsure the passed attribute is a valid comma separated Latitude and Longitude string. For example: 51.507877,-0.087732.
DomainRestrictedEmailEnsure 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.
ExcludesHtmlEnsure the passed attribute does not contain HTML.
HexColourCodeEnsure 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();
HonorificEnsure the passed attribute is a valid honorific, omitting appended dots. The list of valid honorifics is available here.
IncludesHtmlEnsure the passed attribute contains HTML.
NoWhitespaceEnsure the passed attribute contains no whitespace.
NumberParityValidate 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(),
],
]);
StringContainsEnsure 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.
StrongPasswordEnsure the given attribute matches the provided conditions.
Minimum usage example to ensure the attribute:
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.
TitleCaseEnsure the provided attribute is title case.
UKMobilePhoneEnsure the provided attribute is a valid UK mobile telephone number.
UppercaseEnsure the provided attribute is entirely uppercase.
Any ideas are welcome. Feel free to submit any issues or pull requests.
composer test
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.