- Notifications
You must be signed in to change notification settings - Fork1
A set of useful Laravel validation rules
License
f9webltd/laravel-validation-rules
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
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 atresources/lang/vendor/f9web-validation-rules/messages.php.
As discussed in the officialLaravel documentation, import the required rule whenever required:
useF9Web\ValidationRules\Rules\TitleCase;// ...$request->validate(['team' => ['required',newTitleCase()],]);
Alternatively use the rule directly with aLaravel form request object
Base64EncodedStringCoordinateDomainRestrictedEmailExcludesHtmlHexColourCodeHonorificIncludesHtmlNoWhitespaceNumberParityStringContainsStrongPasswordTitleCaseUKMobilePhoneUppercase
Ensure the passed attribute is a valid base 64 encoded string.
Ensure the passed attribute is a valid comma separated Latitude and Longitude string. For example:51.507877,-0.087732.
Ensure the passed email in question is part of the provided whitelist of domains.
For instance, to ensure the given email domain isf9web.co.uk orlaravel.com:
useF9Web\ValidationRules\Rules\DomainRestrictedEmail;// ...$request->validate(['email' => ['required', (newDomainRestrictedEmail())->validDomains(['f9web.co.uk','laravel.com', ]), ],]);
The validation message will include the list of whitelisted domains based upon the provided configuration.
Ensure the passed attribute does not contain HTML.
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:
useF9Web\ValidationRules\Rules\HexColourCode;(newHexColourCode());
Extended usage example to validate a long length code , omitting prefix i.e.cc0000:
useF9Web\ValidationRules\Rules\HexColourCode;(newHexColourCode())->withoutPrefix()->longFormat();
Ensure the passed attribute is a valid honorific, omitting appended dots. The list of valid honorifics is availablehere.
Ensure the passed attribute contains HTML.
Ensure the passed attribute contains no whitespace.
Validate the number parity.
An odd number:
useF9Web\ValidationRules\Rules\NumberParity;// ...$request->validate(['amount' => ['required', (newNumberParity())->odd(), ],]);
An even number:
useF9Web\ValidationRules\Rules\NumberParity;// ...$request->validate(['amount' => ['required', (newNumberParity())->even(), ],]);
Ensure the given attribute contains the provided strings.
Minimum usage example to ensure the attribute in question contains the stringphp orlaravel:
useF9Web\ValidationRules\Rules\StringContains;// ...$request->validate(['description' => ['required', (newStringContains())->phrases(['laravel','php', ]), ],]);
Optionally force the string to containall provided phrases:
useF9Web\ValidationRules\Rules\StringContains;// ...$request->validate(['description' => ['required', (newStringContains())->phrases(['laravel','php', ])->strictly(), ],]);
The validation message will include the list phrases based upon the provided configuration.
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
useF9Web\ValidationRules\Rules\StrongPassword;// ...$request->validate(['password' => ['required', (newStrongPassword()), ],]);
Additional methods are available.
useF9Web\ValidationRules\Rules\StrongPassword;// ...$request->validate(['password' => ['required', (newStrongPassword()) ->forceUppercaseCharacters() ->forceLowercaseCharacters(false) ->forceNumbers() ->forceSpecialCharacters()// ->withSpecialCharacters('£$*%^'), ],]);
The default special characters are!@#$%^&*()\-_=+{};:,<."£~?|>. Optionally thewithSpecialCharacters() method can be used to define a custom list.
Ensure the provided attribute istitle case.
Ensure the provided attribute is a valid UK mobile telephone number.
Ensure the provided attribute is entirely uppercase.
Any ideas are welcome. Feel free to submit any issues or pull requests.
composertestIf you discover any security related issues, please emailrob@f9web.co.uk instead of using the issue tracker.
The MIT License (MIT). Please seeLicense File for more information.
About
A set of useful Laravel validation rules
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.