milwad-dev/laravel-validate

Repository files navigation

laravel-validate-banner

PHP Version RequireLatest Stable VersionTotal DownloadsLicensePassed TestsQuality ScoreCheck ImportsAsk DeepWiki


The Laravel-Validate package enhanced Laravel validation capabilities with custom rules and methods for simplified and efficient validation logic.
You can validate data more easily. The Laravel Validate has lots of rule classes for validation.
This package supports localization and can be used for most languages.
(If some language didn't support, you can create PR for new language)

AI Documentation


  • PHP >= 8.0
  • Laravel >= 9.0
L6L7L8L9L10L11L12
1.5
1.6
1.7
1.8
1.9

You can install the package with Composer.

composer require milwad/laravel-validate

If you want to publish a config file, you can run the following command on your terminal:

php artisan vendor:publish --tag="laravel-validate-config"

If you want to publish a lang file for a custom validation message, you can run this command in the terminal:

php artisan vendor:publish --tag="validate-lang-{$lang}"

You can replace $lang with your language name, for example:

php artisan vendor:publish --tag="validate-lang-en"

You can see the Support Languages section if you don't know the language's name.

If you may use rules with a string like ValidPhone, you need to change the config option to true:

/*
 * If you want to use rules like 'required|ValidPhone' in your validations, you can change it to true.
 */
'using_container' => false,

If using_container is set to true, you might have rules like this:

'phone_number' => 'required|ValidPhone',

And ValidPhone would be a class that is resolved via the service container to check the validity of the phone number.

You can use Laravel-Validate Rules very simply. You can use the new keyword before the rule name.

use Milwad\LaravelValidate\Rules\ValidPhoneNumber;

return [
    'phone' => ['required', new ValidPhoneNumber()],
];
#Rule nameUsage
1ValidBase64Validate base64 for ex (bWlsd2Fk)
2ValidBitcoinAddressValidate bitcoin address for ex (1KFHE7w8BhaENAswwryaoccDb6qcT6DbYY)
3ValidCamelCaseValidate camel case for ex (milwadDev)
4ValidCapitalCharWithNumberValidate String for ex (MILWAD-84)
5ValidCarNumberValidate car number for ex (KA01AB1234)
6ValidCartNumberIranValidate cart number for ex (1234123412341234)
7ValidCountryValidate country name for ex (United Arab Emirates)
8ValidCreditCardValidate credit card for ex (4111111111111111)
9ValidDiscordUsernameValidate username discord for ex (Milwad#2134)
10ValidDomainValidate domain name for ex (.com)
11ValidDuplicateValidate duplicate string, numbers for ex (1123456)
12ValidDuplicateCharacterValidate duplicate characters for ex (1,2,3,4,5,6,7,8,9)
13ValidEvenNumberValidate number is even for ex (1024)
14ValidHashtagValidate hashtag for ex (#milwad)
15ValidHexColorValidate hex color for ex (#fcba03)
16ValidHtmlTagValidate html tags for ex (<h1></h1>)
17ValidIbanValidate IBAN ex (IR062960000000100324200001)
18ValidImeiValidate imei phone for ex (354809104295874)
19ValidIpAddressIPV4Validate ip address IPV4 for ex (129.144.50.56)
20ValidIpAddressIPV6Validate ip address IPV6 for ex (2001:db8:3333:4444:5555:6666:7777:8888)
21ValidIranPostalCodeValidate Iran postal code ex (3354355599)
22ValidJalaliDateValidate jalali date for ex (1384/8/25)
23ValidJwtValidate jwt for ex (eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEyMzQ1Njc4OTAiLCJuYW1lIjoiSm9obiBEb2UiLCJhZG1pbiI6dHJ1ZSwiZXhwIjoxNTgyNjE2MDA1fQ.umEYVDP_kZJGCI3tkU9dmq7CIumEU8Zvftc-klp-334)
24ValidKebabCaseValidate kebab case for ex (milwad-dev)
25ValidNameDaysWeekValidate day week for ex (monday)
26ValidNationalCardValidate national card for ex (015016437)
27ValidOddNumberValidate number is odd for ex (4321)
28ValidPascalCaseValidate pascal case for ex (MilwadDev)
29ValidPatternValidate texts with specific pattern 🔥 for ex (4444-4444-4444)
30ValidPhoneNumberValidate phone number for ex (09366000000)
31ValidPortValidate port value for ex (8080)
32ValidSlashEndOfStringValidate write slash at the string for ex (milwad/)
33ValidSlugValidate slug for ex (milwad-dev)
34ValidSnakeCaseValidate snake case for ex (milwad_dev)
35ValidStrongPasswordValidate password for ex (Milwad123!)
36ValidUlidValidate ulid for ex (01ARZ3NDEKTSV4RRFFQ69G5FAV)
37ValidUrlValidate url for ex (https://www.google.com)
38ValidUsernameValidate username for ex (milwad)
39ValidUuidValidate uuid for ex (123e4567-e89b-12d3-a456-426655440000)
40ValidVatIdValidate european VAT ID ex (EL123456789123)
41ValidLandlineNumberValidate landline number

If a language is not supported, you can make issue to add it.
Also, you can make PR to add it.

  • Arabic (ar)
  • Azerbaijani (az)
  • Bangla (bn)
  • Catalan (ca)
  • Dutch (nl)
  • German (de)
  • Greek (el)
  • English (en)
  • Spanish (es)
  • Persian (fa)
  • French (fr)
  • Hindi (hi)
  • Indonesian (id)
  • Italian (It)
  • Japanese (ja)
  • Korean (ko)
  • Kurdish Sorani (ku_so)
  • Português Brasil (pt_BR)
  • Russian (ru)
  • Sinhala (si)
  • Swedish (sv)
  • Turkish (tr)
  • Ukrainian (uk)
  • Chinese (zh_CN)

If you need to add a custom phone number validator for a specific country, follow the steps below.

First, you need to create a custom validator class that implements the Milwad\LaravelValidate\Utils\CountryPhoneValidator contract. This contract ensures that your custom validator adheres to the required structure and functionality.

namespace App\Validators;

use Milwad\LaravelValidate\Utils\CountryPhoneValidator;

class CustomPhoneValidator implements CountryPhoneValidator
{
    /**
     * Validate the phone number for the custom country.
     */
    public function validate(string $phoneNumber): bool
    {
        // Implement the phone number validation logic for your country
        // Example: Check if the phone number matches a specific pattern
        return preg_match('/^\+1234\d{10}$/', $phoneNumber);
    }
}

Once you've created the custom validator class, add it to the configuration file (config/laravel-validate.php) under the 'phone-country' array.

For example, if you're adding a validator for the country XY:

'phone-country' => [
    // Existing validators...
    'XY' => \App\Validators\CustomPhoneValidator::class, // Custom country
],

This tells the system to use your custom validator for phone numbers from country XY.

Once your custom validator is set up, you can use it in your application like any other validator:

return [
    'phone_ir' => [new ValidPhoneNumber('XY')],
];
  • This package is created and modified by Milwad Khosravi for Laravel over more than 9 and has been released under the MIT License.

This project exists thanks to all the people who contribute. CONTRIBUTING

If you've found a security bug, please email [email protected] instead of using the issue tracker.

Star History Chart

If this package is helpful for you, you can buy a coffee for me :) ❤️