PHPackages                             simtabi/enekia - 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. simtabi/enekia

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

simtabi/enekia
==============

A set of useful additional validation rules for the Laravel framework

0134PHP

Since Feb 26Pushed 3y ago1 watchersCompare

[ Source](https://github.com/simtabi/enekia)[ Packagist](https://packagist.org/packages/simtabi/enekia)[ RSS](/packages/simtabi-enekia/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

 [ ![](https://avatars1.githubusercontent.com/u/47185924) ](https://github.com/simtabi)

Enekia: Laravel Validation Rules
================================

[](#enekia-laravel-validation-rules)

Intervention Validation
=======================

[](#intervention-validation)

Intervention Validation is an extension library for Laravel's own validation system. The package adds rules to validate data like IBAN, BIC, ISBN, creditcard numbers and more.

[![Latest Version](https://camo.githubusercontent.com/7b038900ef054ff515ead7cef1ad12706a4456ff30400a562912c20bcd41e840/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696e74657276656e74696f6e2f76616c69646174696f6e2e737667)](https://packagist.org/packages/intervention/validation)[![build](https://github.com/Intervention/validation/workflows/build/badge.svg)](https://github.com/Intervention/validation/workflows/build/badge.svg)[![Monthly Downloads](https://camo.githubusercontent.com/14648712dc828cbbfc729cbbc8f972856e64f352ad4d1ef13c0e8d4b346b5e87/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f696e74657276656e74696f6e2f76616c69646174696f6e2e737667)](https://packagist.org/packages/intervention/validation/stats)

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

[](#installation)

You can install this package quick and easy with Composer.

Require the package via Composer:

```
$ composer require intervention/validation

```

Laravel integration
-------------------

[](#laravel-integration)

The Validation library is built to work with the Laravel Framework (&gt;=7). It comes with a service provider, which will be discovered automatically and registers the validation rules into your installation. The package provides 30 additional validation rules including error messages, which can be used like Laravel's own validation rules.

```
use Illuminate\Support\Facades\Validator;
use Simtabi\Enekia\Laravel\Traits\Rules\CreditCard;
use Simtabi\Enekia\Laravel\Traits\Rules\HexColor;
use Simtabi\Enekia\Laravel\Traits\Rules\Username;

$validator = Validator::make($request->all(), [
    'color' => new HexColor(3), // pass rule as object
    'number' => ['required', 'creditcard'], // or pass rule as string
    'name' => 'required|min:3|max:20|username', // combining rules works as well
]);
```

### Changing the error messages:

[](#changing-the-error-messages)

Add the corresponding key to `/resources/lang//validation.php` like this:

```
// example
'iban' => 'Please enter IBAN number!',
```

Or add your custom messages directly to the validator like [described in the docs](https://laravel.com/docs/8.x/validation#manual-customizing-the-error-messages).

Standalone usage
----------------

[](#standalone-usage)

It is also possible to use this library without the Laravel framework. You won't have the Laravel facades available, so make sure to use `Simtabi\Enekia\Validator` for your calls.

```
use Simtabi\Enekia\Validator;
use Simtabi\Enekia\Laravel\Traits\Rules\CreditCard;
use Simtabi\Enekia\Laravel\Exceptions\EnekiaException;

// use static factory method to create laravel validator
$validator = Validator::make($request->all(), [
    'ccnumber' => new CreditCard(),
    'iban' => ['required', 'iban'],
    'color' => 'required|hexcolor:3',
]);

// validate single values by calling static methods
$result = Validator::isHexcolor('foobar'); // false
$result = Validator::isHexcolor('#ccc'); // true
$result = Validator::isBic('foo'); // false

// assert single values
try {
    Validator::assertHexcolor('foobar');
} catch (EnekiaException $e) {
    $message = $e->getMessage();
}
```

Available Rules
===============

[](#available-rules)

The following validation rules are available with this package.

Base64 encoded string
---------------------

[](#base64-encoded-string)

The field under validation must be [Base64 encoded](https://en.wikipedia.org/wiki/Base64).

```
public Simtabi\Enekia\Laravel\Traits\Rules\Base64::__construct()

```

Business Identifier Code (BIC)
------------------------------

[](#business-identifier-code-bic)

Checks for a valid [Business Identifier Code](https://en.wikipedia.org/wiki/ISO_9362) (BIC).

```
public Simtabi\Enekia\Laravel\Traits\Rules\Bic::__construct()

```

Camel case string
-----------------

[](#camel-case-string)

The field under validation must be a formated in [Camel case](https://en.wikipedia.org/wiki/Camel_case).

```
public Simtabi\Enekia\Laravel\Traits\Rules\Camelcase::__construct()

```

Classless Inter-Domain Routing (CIDR)
-------------------------------------

[](#classless-inter-domain-routing-cidr)

Check if the value is a [Classless Inter-Domain Routing](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing) notation (CIDR).

```
public Simtabi\Enekia\Laravel\Traits\Rules\Cidr::__construct()

```

Creditcard Number
-----------------

[](#creditcard-number)

The field under validation must be a valid [creditcard number](https://en.wikipedia.org/wiki/Payment_card_number).

```
public Simtabi\Enekia\Laravel\Traits\Rules\Creditcard::__construct()

```

Data URI scheme
---------------

[](#data-uri-scheme)

The field under validation must be a valid [Data URI](https://en.wikipedia.org/wiki/Data_URI_scheme).

```
public Simtabi\Enekia\Laravel\Traits\Rules\DataUri::__construct()

```

Domain name
-----------

[](#domain-name)

The field under validation must be a well formed [domainname](https://en.wikipedia.org/wiki/Domain_name).

```
public Simtabi\Enekia\Laravel\Traits\Rules\Domainname::__construct()

```

European Article Number (EAN)
-----------------------------

[](#european-article-number-ean)

Checks for a valid [European Article Number](https://en.wikipedia.org/wiki/International_Article_Number).

```
public Simtabi\Enekia\Laravel\Traits\Rules\Ean::__construct(?int $length = null)

```

#### Parameters

[](#parameters)

**length**

Optional integer length (8 or 13) to check only for EAN-8 or EAN-13.

Global Trade Item Number (GTIN)
-------------------------------

[](#global-trade-item-number-gtin)

Checks for a valid [Global Trade Item Number](https://en.wikipedia.org/wiki/Global_Trade_Item_Number).

```
public Simtabi\Enekia\Laravel\Traits\Rules\Gtin::__construct(?int $length = null)

```

#### Parameters

[](#parameters-1)

**length**

Optional integer length to check only for certain types (GTIN-8, GTIN-12, GTIN-13 or GTIN-14).

Hexadecimal color code
----------------------

[](#hexadecimal-color-code)

The field under validation must be a valid [hexadecimal color code](https://en.wikipedia.org/wiki/Web_colors).

```
public Simtabi\Enekia\Laravel\Traits\Rules\HexColor::__construct(?int $length = null)

```

#### Parameters

[](#parameters-2)

**length**

Optional length as integer to check only for shorthand (3 characters) or full hexadecimal (6 characters) form.

Text without HTML
-----------------

[](#text-without-html)

The field under validation must be free of any html code.

```
public Simtabi\Enekia\Laravel\Traits\Rules\HtmlClean::__construct()

```

International Bank Account Number (IBAN)
----------------------------------------

[](#international-bank-account-number-iban)

Checks for a valid [International Bank Account Number](https://en.wikipedia.org/wiki/International_Bank_Account_Number) (IBAN).

```
public Simtabi\Enekia\Laravel\Traits\Rules\Iban::__construct()

```

International Mobile Equipment Identity (IMEI)
----------------------------------------------

[](#international-mobile-equipment-identity-imei)

The field under validation must be a [International Mobile Equipment Identity](https://en.wikipedia.org/wiki/International_Mobile_Equipment_Identity) (IMEI).

```
public Simtabi\Enekia\Laravel\Traits\Rules\Imei::__construct()

```

International Standard Book Number (ISBN)
-----------------------------------------

[](#international-standard-book-number-isbn)

The field under validation must be a valid [International Standard Book Number](https://en.wikipedia.org/wiki/International_Standard_Book_Number) (ISBN).

```
public Simtabi\Enekia\Laravel\Traits\Rules\Isbn::__construct(?int $length = null)

```

#### Parameters

[](#parameters-3)

**length**

Optional length parameter as integer to check only for ISBN-10 or ISBN-13.

International Securities Identification Number (ISIN)
-----------------------------------------------------

[](#international-securities-identification-number-isin)

Checks for a valid [International Securities Identification Number](https://en.wikipedia.org/wiki/International_Securities_Identification_Number) (ISIN).

```
public Simtabi\Enekia\Laravel\Traits\Rules\Isin::__construct()

```

International Standard Serial Number (ISSN)
-------------------------------------------

[](#international-standard-serial-number-issn)

Checks for a valid [International Standard Serial Number](https://en.wikipedia.org/wiki/International_Standard_Serial_Number) (ISSN).

```
public Simtabi\Enekia\Laravel\Traits\Rules\Issn::__construct()

```

JSON Web Token (JWT)
--------------------

[](#json-web-token-jwt)

The given value must be a in format of a [JSON Web Token](https://en.wikipedia.org/wiki/JSON_Web_Token).

```
public Simtabi\Enekia\Laravel\Traits\Rules\Jwt::__construct()

```

Kebab case string
-----------------

[](#kebab-case-string)

The given value must be formated in [Kebab case](https://en.wikipedia.org/wiki/Letter_case#Special_case_styles).

```
public Simtabi\Enekia\Laravel\Traits\Rules\Kebabcase::__construct()

```

Lower case string
-----------------

[](#lower-case-string)

The given value must be all lower case letters.

```
public Simtabi\Enekia\Laravel\Traits\Rules\Lowercase::__construct()

```

Luhn algorithm
--------------

[](#luhn-algorithm)

The given value must verify against its included [Luhn algorithm](https://en.wikipedia.org/wiki/Luhn_algorithm) check digit.

```
public Simtabi\Enekia\Laravel\Traits\Rules\Luhn::__construct()

```

Media (MIME) type
-----------------

[](#media-mime-type)

Checks for a valid [Mime Type](https://en.wikipedia.org/wiki/Media_type) (Media type).

```
public Simtabi\Enekia\Laravel\Traits\Rules\MimeType::__construct()

```

Postal Code
-----------

[](#postal-code)

The field under validation must be a [postal code](https://en.wikipedia.org/wiki/Postal_code) of the given country.

```
public Simtabi\Enekia\Laravel\Traits\Rules\Postalcode::__construct(string $countrycode)

```

#### Parameters

[](#parameters-4)

**countrycode**

Country code in [ISO-639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.

### Postal Code (static instantiation)

[](#postal-code-static-instantiation)

```
public static Simtabi\Enekia\Laravel\Traits\Rules\Postalcode::countrycode(string $countrycode): Postalcode

```

#### Parameters

[](#parameters-5)

**countrycode**

Country code in [ISO-639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.

### Postal Code (static instantiation with callback)

[](#postal-code-static-instantiation-with-callback)

```
public static Simtabi\Enekia\Laravel\Traits\Rules\Postalcode::resolve(callable $callback): Postalcode

```

#### Parameters

[](#parameters-6)

**callback**

Callback to resolve [ISO-639-1](https://en.wikipedia.org/wiki/ISO_639-1) country code from other source.

### Postal Code (static instantiation with reference)

[](#postal-code-static-instantiation-with-reference)

```
public static Simtabi\Enekia\Laravel\Traits\Rules\Postalcode::reference(string $reference): Postalcode

```

#### Parameters

[](#parameters-7)

**reference**

Reference key to get [ISO-639-1](https://en.wikipedia.org/wiki/ISO_639-1) country code from other data in validator.

Semantic Version Number
-----------------------

[](#semantic-version-number)

The field under validation must be a valid version numbers using [Semantic Versioning](https://semver.org/).

```
public Simtabi\Enekia\Laravel\Traits\Rules\SemVer::__construct()

```

SEO-friendly short text (Slug)
------------------------------

[](#seo-friendly-short-text-slug)

The field under validation must be a user- and [SEO-friendly short text](https://en.wikipedia.org/wiki/Clean_URL#Slug).

```
public Simtabi\Enekia\Laravel\Traits\Rules\Slug::__construct()

```

Snake case string
-----------------

[](#snake-case-string)

The field under validation must formated as [Snake case](https://en.wikipedia.org/wiki/Snake_case) text.

```
public Simtabi\Enekia\Laravel\Traits\Rules\Snakecase::__construct()

```

Title case string
-----------------

[](#title-case-string)

The field under validation must formated in [Title case](https://en.wikipedia.org/wiki/Title_case).

```
public Simtabi\Enekia\Laravel\Traits\Rules\Titlecase::__construct()

```

Universally Unique Lexicographically Sortable Identifier (ULID)
---------------------------------------------------------------

[](#universally-unique-lexicographically-sortable-identifier-ulid)

The field under validation must be a valid [Universally Unique Lexicographically Sortable Identifier](https://github.com/ulid/spec).

```
public Simtabi\Enekia\Laravel\Traits\Rules\Ulid::__construct()

```

Upper case string
-----------------

[](#upper-case-string)

The field under validation must be all upper case.

```
public Simtabi\Enekia\Laravel\Traits\Rules\Uppercase::__construct()

```

Username
--------

[](#username)

The field under validation must be a valid username. Consisting of alpha-numeric characters, underscores, minus and starting with a alphabetic character. Multiple underscore and minus chars are not allowed. Underscore and minus chars are not allowed at the beginning or end.

```
public Simtabi\Enekia\Laravel\Traits\Rules\Username::__construct()

```

### `AuthorizedOnModelAction`

[](#authorizedonmodelaction)

Determine if the user is authorized to perform an ability on an instance of the given model. The id of the model is the field under validation

Consider the following policy:

```
class ModelPolicy
{
    use HandlesAuthorization;

    public function edit(User $user, Model $model): bool
    {
        return $model->user->id === $user->id;
    }
}
```

This validation rule will pass if the id of the logged in user matches the `user_id` on `TestModel` who's it is in the `model_id` key of the request.

```
// in a `FormRequest`

use Simtabi\Enekia\Laravel\Traits\Rules\AuthorizedOnModelAction;

public function rules()
{
    return [
        'model_id' => [new AuthorizedOnModelAction('edit', TestModel::class)],
    ];
}
```

Optionally, you can provide an authentication guard as the third parameter.

```
new AuthorizedOnModelAction('edit', TestModel::class, 'guard-name')
```

#### Model resolution

[](#model-resolution)

If you have implemented the `getRouteKeyName` method in your model, it will be used to resolve the model instance. For further information see [Customizing The Default Key Name](https://laravel.com/docs/7.x/routing)

### `CountryCode`

[](#countrycode)

Determine if the field under validation is a valid [2 letter ISO3166 country code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Current_codes) (example of valid country codes: `GB`, `DK`, `NL`).

**Note** that this rule require the package [`league/iso3166`](https://github.com/thephpleague/iso3166) to be installed: `composer require league/iso3166`

```
// in a `FormRequest`

use Simtabi\Enekia\Laravel\Traits\Rules\Country;

public function rules()
{
    return [
        'country_code' => ['required', new Country()],
    ];
}
```

If you want to validate a nullable country code field, you can call the `nullable()` method on the `CountryCode` rule. This way `null` and `0` are also passing values:

```
// in a `FormRequest`

use Simtabi\Enekia\Laravel\Traits\Rules\Country;

public function rules()
{
    return [
        'country_code' => [(new Country())->nullable()],
    ];
}
```

### `Currency`

[](#currency)

Determine if the field under validation is a valid [3 letter ISO4217 currency code](https://en.wikipedia.org/wiki/ISO_4217#Active_codes) (example of valid currencies: `EUR`, `USD`, `CAD`).

**Note** that this rule require the package [`league/iso3166`](https://github.com/thephpleague/iso3166) to be installed: `composer require league/iso3166`

```
// in a `FormRequest`

use Simtabi\Enekia\Laravel\Traits\Rules\Currency;

public function rules()
{
    return [
        'currency' => ['required', new Currency()], // Must be present and a valid currency
    ];
}
```

If you want to validate a nullable currency field, simple do not let it be required as described in the [Laravel Docs for implicit validation rules](https://laravel.com/docs/master/validation#implicit-rules):

> ... when an attribute being validated is not present or contains an empty string, normal validation rules, including custom rules, are not run

```
// in a `FormRequest`

use Simtabi\Enekia\Laravel\Traits\Rules\Currency;

public function rules()
{
    return [
        'currency' => [new Currency()], // This will pass for any valid currency, an empty value or null
    ];
}
```

### `ModelsExist`

[](#modelsexist)

Determine if all the values in the input array exist as attributes for the given model class.

By default, the rule assumes that you want to validate using `id` attribute. In the example below the validation will pass if all `model_ids` exist for the `Model`.

```
// in a `FormRequest`

use Simtabi\Enekia\Laravel\Traits\Rules\ModelIdsExist;

public function rules()
{
    return [
        'model_ids' => ['array', new ModelIdsExist(Model::class)],
    ];
}
```

You can also pass an attribute name as the second argument. In the example below the validation will pass if there are users for each email given in the `user_emails` of the request.

```
// in a `FormRequest`

use Simtabi\Enekia\Laravel\Traits\Rules\ModelIdsExist;

public function rules()
{
    return [
        'user_emails' => ['array', new ModelIdsExist(User::class, 'emails')],
    ];
}
```

### `Delimited`

[](#delimited)

This rule can validate a string containing delimited values. The constructor accepts a rule that is used to validate all separate values.

Here's an example where we are going to validate a string containing comma separated email addresses.

```
// in a `FormRequest`

use Simtabi\Enekia\Laravel\Traits\Rules\Delimited;

public function rules()
{
    return [
        'emails' => [new Delimited('email')],
    ];
}
```

Here's some example input that passes this rule:

- `'sebastian@example.com, alex@example.com'`
- `''`
- `'sebastian@example.com'`
- `'sebastian@example.com, alex@example.com, brent@example.com'`
- `' sebastian@example.com   , alex@example.com  ,   brent@example.com  '`

This input will not pass:

- `'@example.com'`
- `'nocomma@example.com nocommatoo@example.com'`
- `'valid@example.com, invalid@'`

#### Setting a minimum

[](#setting-a-minimum)

You can set minimum amout of items that should be present:

```
(new Delimited('email'))->min(2)
```

- `'sebastian@example.com, alex@example.com'` // passes
- `'sebastian@example.com'` // fails

#### Setting a maximum

[](#setting-a-maximum)

```
(new Delimited('email'))->max(2)
```

- `'sebastian@example.com'` // passes
- `'sebastian@example.com, alex@example.com, brent@example.com'` // fails

#### Allowing duplicate items

[](#allowing-duplicate-items)

By default the rule will fail if there are duplicate items found.

- `'sebastian@example.com, sebastian@example.com'` // fails

You can allowing duplicate items like this:

```
(new Delimited('numeric'))->allowDuplicates()
```

Now this will pass: `1,1,2,2,3,3`

#### Customizing the separator

[](#customizing-the-separator)

```
(new Delimited('email'))->separatedBy(';')
```

- `'sebastian@example.com; alex@example.com; brent@example.com'` // passes
- `'sebastian@example.com, alex@example.com, brent@example.com'` // fails

#### Skip trimming of items

[](#skip-trimming-of-items)

```
(new Delimited('email'))->doNotTrimItems()
```

- `'sebastian@example.com,freek@example.com'` // passes
- `'sebastian@example.com, freek@example.com'` // fails
- `'sebastian@example.com , freek@example.com'` // fails

#### Composite rules

[](#composite-rules)

The constructor of the validator accepts a validation rule string, a validate instance, or an array.

```
new Delimited('email|max:20')
```

- `'short@example.com'` // passes
- `'invalid'` // fails
- `'loooooooonnnggg@example.com'` // fails

#### Passing custom error messages

[](#passing-custom-error-messages)

The constructor of the validator accepts a custom error messages array as second parameter.

```
// in a `FormRequest`

use Simtabi\Enekia\Laravel\Traits\Rules\Delimited;

public function rules()
{
    return [
        'emails' => [new Delimited('email', $this->messages())],
    ];
}

public function messages()
{
    return [
        'emails.email' => 'Not all the given e-mails are valid.',
    ];
}
```

Laravel Model Exists Rule
=========================

[](#laravel-model-exists-rule)

[![Latest Version on Packagist](https://camo.githubusercontent.com/b44b95d41d11ce7424781e441b95d6d98d1661424325d7f84ad7baa32ed8fd2c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d76616e6475696a6b65722f6c61726176656c2d6d6f64656c2d6578697374732d72756c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mvanduijker/laravel-model-exists-rule)[![Build Status](https://github.com/mvanduijker/laravel-model-exists-rule/workflows/Run%20tests/badge.svg)](https://github.com/mvanduijker/laravel-model-exists-rule/workflows/Run%20tests/badge.svg)[![Total Downloads](https://camo.githubusercontent.com/f993562053550ae9aad0fd3cc72aa9d1cf4a1a2b7d01c1c8af1bdb66cc1ffe93/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d76616e6475696a6b65722f6c61726176656c2d6d6f64656c2d6578697374732d72756c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mvanduijker/laravel-model-exists-rule)

Laravel validation rule to check if a model exists.

You want to use this rule if the standard laravel `Rule::exists('table', 'column')` is not powerful enough. When you want to add joins to your exist rule, or the advanced Eloquent\\Builder features like whereHas this might be for you.

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

[](#installation-1)

You can install the package via composer:

```
composer require mvanduijker/laravel-model-exists-rule
```

Usage
-----

[](#usage)

Simple

```
