Skip to content

The Laravel-Validate package enhanced Laravel validation capabilities with custom rules and methods for simplified and efficient validation logic

License

Notifications You must be signed in to change notification settings

milwad-dev/laravel-validate

Repository files navigation

Laravel Validate

laravel-validate-banner

PHP Version Require Latest Stable Version Total Downloads License Passed Tests Quality Score Check Imports


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, Laravel validate has lots of rule classes for validation.
This package supports localization and can be used for most of the language.
(If some language didn't support, you can PR for new language)

Requirements


  • PHP >= 8.0
  • Laravel >= 9.0
GitHub L6 L7 L8 L9 L10 L11
1.5
1.6
1.7
1.8

Installation

You can install the package with Composer.

composer require milwad/laravel-validate

Publish

If you want to publish config file, you can run below 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 lang name, for example:

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

If you don't know about langs name you can see Support Languages section.

Configurations

If you may use rules with 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.

Usage

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()],
];

Rules

# Rule name Usage
1 ValidBase64 Validate base64 for ex (bWlsd2Fk)
2 ValidBitcoinAddress Validate bitcoin address for ex (1KFHE7w8BhaENAswwryaoccDb6qcT6DbYY)
3 ValidCamelCase Validate camel case for ex (milwadDev)
4 ValidCapitalCharWithNumber Validate String for ex (MILWAD-84)
5 ValidCarNumber Validate car number for ex (KA01AB1234)
6 ValidCartNumberIran Validate cart number for ex (1234123412341234)
7 ValidCreditCard Validate credit card for ex (4111111111111111)
8 ValidDiscordUsername Validate username discord for ex (Milwad#2134)
9 ValidDomain Validate domain name for ex (github.com)
10 ValidDuplicate Validate duplicate string, numbers for ex (1123456)
11 ValidDuplicateCharacter Validate duplicate characters for ex (1,2,3,4,5,6,7,8,9)
12 ValidEvenNumber Validate number is even for ex (1024)
13 ValidHashtag Validate hashtag for ex (#milwad)
14 ValidHexColor Validate hex color for ex (#fcba03)
15 ValidHtmlTag Validate html tags for ex (<h1></h1>)
16 ValidIban Validate IBAN ex (IR062960000000100324200001)
17 ValidImei Validate imei phone for ex (354809104295874)
18 ValidIpAddressIPV4 Validate ip address IPV4 for ex (129.144.50.56)
19 ValidIpAddressIPV6 Validate ip address IPV6 for ex (2001:db8:3333:4444:5555:6666:7777:8888)
20 ValidIranPostalCode Validate Iran postal code ex (3354355599)
21 ValidJalaliDate Validate jalali date for ex (1384/8/25)
22 ValidJwt Validate jwt for ex (eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEyMzQ1Njc4OTAiLCJuYW1lIjoiSm9obiBEb2UiLCJhZG1pbiI6dHJ1ZSwiZXhwIjoxNTgyNjE2MDA1fQ.umEYVDP_kZJGCI3tkU9dmq7CIumEU8Zvftc-klp-334)
23 ValidKebabCase Validate kebab case for ex (milwad-dev)
24 ValidNameDaysWeek Validate day week for ex (monday)
25 ValidNationalCard Validate national card for ex (015016437)
26 ValidOddNumber Validate number is odd for ex (4321)
27 ValidPascalCase Validate pascal case for ex (MilwadDev)
28 ValidPattern Validate texts with specific pattern 🔥 for ex (4444-4444-4444)
29 ValidPhoneNumber Validate phone number for ex (09366000000)
30 ValidPort Validate port value for ex (8080)
31 ValidSlashEndOfString Validate write slash at the string for ex (milwad/)
32 ValidSlug Validate slug for ex (milwad-dev)
33 ValidSnakeCase Validate snake case for ex (milwad_dev)
34 ValidStrongPassword Validate password for ex (Milwad123!)
35 ValidUlid Validate ulid for ex (01ARZ3NDEKTSV4RRFFQ69G5FAV)
36 ValidUrl Validate url for ex (https://www.google.com)
37 ValidUsername Validate username for ex (milwad)
38 ValidUuid Validate uuid for ex (123e4567-e89b-12d3-a456-426655440000)
39 ValidVatId Validate european VAT ID ex (EL123456789123)

Support Languages

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)
  • 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)

Adding Custom Phone Country Validator

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

1. Create Your Custom Validator Class

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);
    }
}

2. Add the Validator to the Configuration File

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.

3. Validation Usage

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

return [
    'phone_ir' => [new ValidPhoneNumber('XY')],
];

License

  • This package is created and modified by Milwad Khosravi for Laravel over more than 9 and has been released under the MIT License.

Contributing

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

Security

If you've found a security bug, please email milwad.dev@gmail.com instead of using the issue tracker.

Star History

Star History Chart

Donate

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