PHPackages                             arifszn/laravel-advanced-validation - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. arifszn/laravel-advanced-validation

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

arifszn/laravel-advanced-validation
===================================

Laravel advanced validation rules for real-life scenarios.

v1.1.1(4y ago)15381MITPHPPHP ^7.2|^8.0

Since Feb 11Pushed 4y ago1 watchersCompare

[ Source](https://github.com/arifszn/laravel-advanced-validation)[ Packagist](https://packagist.org/packages/arifszn/laravel-advanced-validation)[ RSS](/packages/arifszn-laravel-advanced-validation/feed)WikiDiscussions main Synced today

READMEChangelog (2)Dependencies (3)Versions (5)Used By (0)

Laravel Advanced Validation
===========================

[](#laravel-advanced-validation)

Laravel advanced validation rules for real-life scenarios.

 [![](https://camo.githubusercontent.com/9a5348b013da57e64d17658c517335c0640e0bbaf0ecea6dc25d9573ece01a77/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f61726966737a6e2f6c61726176656c2d616476616e6365642d76616c69646174696f6e)](https://packagist.org/packages/arifszn/laravel-advanced-validation) [![](https://camo.githubusercontent.com/d17f67a0d63f8352c7be899f9433aae4c3c3dfd5abe779242cfa9f29a190ea5a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f61726966737a6e2f6c61726176656c2d616476616e6365642d76616c69646174696f6e)](https://github.com/arifszn/laravel-advanced-validation/blob/main/LICENSE)

Installation
------------

[](#installation)

Install via [composer](https://packagist.org/packages/arifszn/laravel-advanced-validation)

```
composer require arifszn/laravel-advanced-validation
```

Laravel uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.

Translations
------------

[](#translations)

If you wish to edit the package translations, you can run the following command to publish them into your `resources/lang` folder

```
php artisan vendor:publish --provider="Arifszn\AdvancedValidation\ServiceProvider"
```

Custom Error Message
--------------------

[](#custom-error-message)

You can specify the error message on the fly when declaring the rules. Simple pass the error message parameter.

```
use Arifszn\AdvancedValidation\Rules\Username;

public function rules()
{
    return [
        'foo' => [new Username('Your custom error message')],
    ];
}
```

Available Validation Rules
--------------------------

[](#available-validation-rules)

- [`Ascii`](#ascii)
- [`Base64 Image`](#base64image)
- [`Base64 String`](#base64string)
- [`BIC`](#bic)
- [`Btc Address`](#btcaddress)
- [`Credit Card`](#creditcard)
- [`Data URI`](#datauri)
- [`Divisible By`](#divisibleby)
- [`Ethereum Address`](#ethereumaddress)
- [`Float Number`](#floatnumber)
- [`Hash`](#hash)
- [`Image URL`](#imageurl)
- [`JWT`](#jwt)
- [`Name`](#name)
- [`Phone`](#phone)
- [`Username`](#username)
- [`Without Spaces`](#withoutspaces)

### `Ascii`

[](#ascii)

The field under validation must contain ASCII chars only.

```
public Arifszn\AdvancedValidation\Rules\Ascii::__construct(string $errorMessage = null)

```

```
use Arifszn\AdvancedValidation\Rules\Ascii;

public function rules()
{
    return [
        'foo' => [new Ascii()],
    ];
}
```

### `Base64Image`

[](#base64image)

The field under validation must be a Base64 encoded image.

```
public Arifszn\AdvancedValidation\Rules\Base64Image::__construct(string $errorMessage = null)

```

```
use Arifszn\AdvancedValidation\Rules\Base64Image;

public function rules()
{
    return [
        'avatar' => [new Base64Image()],
    ];
}
```

### `Base64String`

[](#base64string)

The field under validation must be a Base64 encoded string.

```
public Arifszn\AdvancedValidation\Rules\Base64String::__construct(string $errorMessage = null)

```

```
use Arifszn\AdvancedValidation\Rules\Base64String;

public function rules()
{
    return [
        'foo' => [new Base64String()],
    ];
}
```

### `BIC`

[](#bic)

The field under validation must be a BIC([Business Identifier Code](https://en.wikipedia.org/wiki/ISO_9362)) or SWIFT code.

```
public Arifszn\AdvancedValidation\Rules\BIC::__construct(string $errorMessage = null)

```

```
use Arifszn\AdvancedValidation\Rules\BIC;

public function rules()
{
    return [
        'foo' => [new BIC()],
    ];
}
```

### `BtcAddress`

[](#btcaddress)

The field under validation must be a valid BTC address.

```
public Arifszn\AdvancedValidation\Rules\BtcAddress::__construct(string $errorMessage = null)

```

```
use Arifszn\AdvancedValidation\Rules\BtcAddress;

public function rules()
{
    return [
        'foo' => [new BtcAddress()],
    ];
}
```

### `CreditCard`

[](#creditcard)

The field under validation must be a valid credit card number.

```
public Arifszn\AdvancedValidation\Rules\CreditCard::__construct(string $errorMessage = null)

```

```
use Arifszn\AdvancedValidation\Rules\CreditCard;

public function rules()
{
    return [
        'foo' => [new CreditCard()],
    ];
}
```

### `DataURI`

[](#datauri)

The field under validation must have [data uri format](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs).

```
public Arifszn\AdvancedValidation\Rules\DataURI::__construct(string $errorMessage = null)

```

```
use Arifszn\AdvancedValidation\Rules\DataURI;

public function rules()
{
    return [
        'foo' => [new DataURI()],
    ];
}
```

### `DivisibleBy`

[](#divisibleby)

The field under validation must be divisible by the given number.

```
public Arifszn\AdvancedValidation\Rules\DivisibleBy::__construct(int $number, string $errorMessage = null)

```

```
use Arifszn\AdvancedValidation\Rules\DivisibleBy;

public function rules()
{
    return [
        'foo' => [new DivisibleBy(2)],
    ];
}
```

### `EthereumAddress`

[](#ethereumaddress)

The field under validation must be an [Ethereum](https://ethereum.org/en/) address. Does not validate address checksums.

```
public Arifszn\AdvancedValidation\Rules\EthereumAddress::__construct(string $errorMessage = null)

```

```
use Arifszn\AdvancedValidation\Rules\EthereumAddress;

public function rules()
{
    return [
        'foo' => [new EthereumAddress()],
    ];
}
```

### `FloatNumber`

[](#floatnumber)

The field under validation must be a float number.

```
public Arifszn\AdvancedValidation\Rules\FloatNumber::__construct(string $errorMessage = null)

```

```
use Arifszn\AdvancedValidation\Rules\FloatNumber;

public function rules()
{
    return [
        'foo' => [new FloatNumber()],
    ];
}
```

### `Hash`

[](#hash)

The field under validation must be a hash of type algorithm.

Algorithm is one of `'md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b'`.

```
public Arifszn\AdvancedValidation\Rules\Hash::__construct(string $algorithm, string $errorMessage = null)

```

```
use Arifszn\AdvancedValidation\Rules\Hash;

public function rules()
{
    return [
        'foo' => [new Hash('md4')],
    ];
}
```

### `ImageURL`

[](#imageurl)

The field under validation must be a valid image URL.

✓
✕

```
public Arifszn\AdvancedValidation\Rules\ImageURL::__construct(string $errorMessage = null)

```

```
use Arifszn\AdvancedValidation\Rules\ImageURL;

public function rules()
{
    return [
        'avatar' => [new ImageURL()],
    ];
}
```

### `JWT`

[](#jwt)

The field under validation must have a valid format of JWT ([JSON Web Token](https://en.wikipedia.org/wiki/JSON_Web_Token)).

```
public Arifszn\AdvancedValidation\Rules\Jwt::__construct(string $errorMessage = null)

```

```
use Arifszn\AdvancedValidation\Rules\Jwt;

public function rules()
{
    return [
        'foo' => [new Jwt()],
    ];
}
```

### `Name`

[](#name)

The field under validation must be a valid name.

- no emoji
- no number (if `$allowNumber` flag is true, it will accept numbers, default is false)
- special characters are allowed (restricting special characters will cause false-negative for names like `Martin Luther King, Jr.` or `李小龍`)

```
public Arifszn\AdvancedValidation\Rules\Name::__construct(bool $allowNumber = false, string $errorMessage = null)

```

```
use Arifszn\AdvancedValidation\Rules\Name;

public function rules()
{
    return [
        'name' => [new Name()],
    ];
}
```

### `Phone`

[](#phone)

The field under validation must be a valid phone number.

✓ +x-xxx-xxx-xxxx
✓ +xxxxxxxxxxx
✓ (xxx) xxx-xxxx
✓ xxxxxxxxxx

```
public Arifszn\AdvancedValidation\Rules\Phone::__construct(string $errorMessage = null)

```

```
use Arifszn\AdvancedValidation\Rules\Phone;

public function rules()
{
    return [
        'foo' => [new Phone()],
    ];
}
```

### `Username`

[](#username)

The field under validation must be a valid username.

- starts with a letter (alpha)
- only alpha-numeric (a-z, A-Z, 0-9), underscore, minus and dot
- multiple underscores, minus and are not allowed (-- or \_\_ or ..)
- underscores, minus and dot are not allowed at the beginning or end

```
public Arifszn\AdvancedValidation\Rules\Username::__construct(string $errorMessage = null)

```

```
use Arifszn\AdvancedValidation\Rules\Username;

public function rules()
{
    return [
        'username' => [new Username()],
    ];
}
```

### `WithoutSpaces`

[](#withoutspaces)

The field under validation must not contain spaces.

```
public Arifszn\AdvancedValidation\Rules\WithoutSpaces::__construct(string $errorMessage = null)

```

```
use Arifszn\AdvancedValidation\Rules\WithoutSpaces;

public function rules()
{
    return [
        'foo' => [new WithoutSpaces()],
    ];
}
```

Tips
----

[](#tips)

If you want to use the rules as strings and use them globally e.g. `'foo' => ['phone']`, you can do so by adding them to the boot method of your project's **AppServiceProvider**.

```
