Extension for the Laravel validation class

Overview

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 build Monthly Downloads

Installation

You can install this package quick and easy with Composer.

Require the package via Composer:

$ composer require intervention/validation

Laravel integration

The Validation library is built to work with the Laravel Framework (>=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.

use Illuminate\Support\Facades\Validator;
use Intervention\Validation\Rules\Creditcard;
use Intervention\Validation\Rules\HexColor;
use Intervention\Validation\Rules\Username;

$validator = Validator::make($request->all(), [
    'color' => new Hexcolor(3), // pass rule as object
    'name' => ['required', 'min:3', 'max:20', new Username()], // combining rules works as well
]);

Make sure to pass any of the additional validation rules as objects and not as strings.

Changing the error messages:

Add the corresponding key to /resources/lang/<language>/validation.php like this:

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

Or add your custom messages directly to the validator like described in the docs.

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 Intervention\Validation\Validator for your calls.

use Intervention\Validation\Validator;
use Intervention\Validation\Rules\Creditcard;
use Intervention\Validation\Exceptions\ValidationException;

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

// 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 (ValidationException $e) {
    $message = $e->getMessage();
}

Available Rules

The following validation rules are available with this package.

Base64 encoded string

The field under validation must be Base64 encoded.

public Intervention\Validation\Rules\Base64::__construct()

Business Identifier Code (BIC)

Checks for a valid Business Identifier Code (BIC).

public Intervention\Validation\Rules\Bic::__construct()

Camel case string

The field under validation must be a formated in Camel case.

public Intervention\Validation\Rules\Camelcase::__construct()

Classless Inter-Domain Routing (CIDR)

Check if the value is a Classless Inter-Domain Routing notation (CIDR).

public Intervention\Validation\Rules\Cidr::__construct()

Creditcard Number

The field under validation must be a valid creditcard number.

public Intervention\Validation\Rules\Creditcard::__construct()

Data URI scheme

The field under validation must be a valid Data URI.

public Intervention\Validation\Rules\DataUri::__construct()

Domain name

The field under validation must be a well formed domainname.

public Intervention\Validation\Rules\Domainname::__construct()

European Article Number (EAN)

Checks for a valid European Article Number.

public Intervention\Validation\Rules\Ean::__construct(?int $length = null)

Parameters

length

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

Global Trade Item Number (GTIN)

Checks for a valid Global Trade Item Number.

public Intervention\Validation\Rules\Gtin::__construct(?int $length = null)

Parameters

length

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

Hexadecimal color code

The field under validation must be a valid hexadecimal color code.

public Intervention\Validation\Rules\HexColor::__construct(?int $length = null)

Parameters

length

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

Text without HTML

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

public Intervention\Validation\Rules\HtmlClean::__construct()

International Bank Account Number (IBAN)

Checks for a valid International Bank Account Number (IBAN).

public Intervention\Validation\Rules\Iban::__construct()

International Mobile Equipment Identity (IMEI)

The field under validation must be a International Mobile Equipment Identity (IMEI).

public Intervention\Validation\Rules\Imei::__construct()

International Standard Book Number (ISBN)

The field under validation must be a valid International Standard Book Number (ISBN).

public Intervention\Validation\Rules\Isbn::__construct(?int $length = null)

Parameters

length

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

International Securities Identification Number (ISIN)

Checks for a valid International Securities Identification Number (ISIN).

public Intervention\Validation\Rules\Isin::__construct()

International Standard Serial Number (ISSN)

Checks for a valid International Standard Serial Number (ISSN).

public Intervention\Validation\Rules\Issn::__construct()

JSON Web Token (JWT)

The given value must be a in format of a JSON Web Token.

public Intervention\Validation\Rules\Jwt::__construct()

Kebab case string

The given value must be formated in Kebab case.

public Intervention\Validation\Rules\Kebabcase::__construct()

Lower case string

The given value must be all lower case letters.

public Intervention\Validation\Rules\Lowercase::__construct()

Luhn algorithm

The given value must verify against its included Luhn algorithm check digit.

public Intervention\Validation\Rules\Luhn::__construct()

MAC address

The field under validation must be a media access control address (MAC address).

public Intervention\Validation\Rules\MacAddress::__construct()

Media (MIME) type

Checks for a valid Mime Type (Media type).

public Intervention\Validation\Rules\MimeType::__construct()

Postal Code

The field under validation must be a postal code of the given country.

public Intervention\Validation\Rules\Postalcode::__construct(string $countrycode)

Parameters

countrycode

Country code in ISO-639-1 format.

Postal Code (static instantiation)

public static Intervention\Validation\Rules\Postalcode::countrycode(string $countrycode): Postalcode

Parameters

countrycode

Country code in ISO-639-1 format.

Postal Code (static instantiation with callback)

public static Intervention\Validation\Rules\Postalcode::resolve(callable $callback): Postalcode

Parameters

callback

Callback to resolve ISO-639-1 country code from other source.

Postal Code (static instantiation with reference)

public static Intervention\Validation\Rules\Postalcode::reference(string $reference): Postalcode

Parameters

reference

Reference key to get ISO-639-1 country code from other data in validator.

Semantic Version Number

The field under validation must be a valid version numbers using Semantic Versioning.

public Intervention\Validation\Rules\SemVer::__construct()

SEO-friendly short text (Slug)

The field under validation must be a user- and SEO-friendly short text.

public Intervention\Validation\Rules\Slug::__construct()

Snake case string

The field under validation must formated as Snake case text.

public Intervention\Validation\Rules\Snakecase::__construct()

Title case string

The field under validation must formated in Title case.

public Intervention\Validation\Rules\Titlecase::__construct()

Universally Unique Lexicographically Sortable Identifier (ULID)

The field under validation must be a valid Universally Unique Lexicographically Sortable Identifier.

public Intervention\Validation\Rules\Ulid::__construct()

Upper case string

The field under validation must be all upper case.

public Intervention\Validation\Rules\Uppercase::__construct()

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 Intervention\Validation\Rules\Username::__construct()

License

Intervention Validation is licensed under the MIT License.

Comments
  • Getting

    Getting "Method Illuminate\Validation\Validator::validateIban does not exist" error

    Hi,

    I'm using laravel 5.8 and after requiring the composer package and using "iban" as a rule it responds with:

    {
        "message": "Method Illuminate\\Validation\\Validator::validateIban does not exist.",
        "exception": "BadMethodCallException",
        "file": "[PATH]/vendor/laravel/framework/src/Illuminate/Validation/Validator.php",
        "line": 1187
    

    Am I missing something?

    Thanks!

    opened by gijsbeijer 14
  • Missing releases

    Missing releases

    Hi,

    Could you please write releases for new tags since the last release ? There is no CHANGELOG and this is blocking me to understand the changes to expect and if I need to upgrade to major 3

    opened by williamdes 3
  • Add new Rule (Date)

    Add new Rule (Date)

    • Created new Class (Date) isInputValid() is checking if the date format valid or not using regex. Formats examples Regex Example

    • Created new ClassTest (DateTest)

    opened by refatalsakka 3
  • CIDR validator/rule

    CIDR validator/rule

    I have written a validator for CIDR content. I think it would be nice to include in your collection instead of building a separate repository for it.

    I have implemented the rule and some basic tests. but I have not finished al translations yet. I have added translations for English and Dutch. I am not fluent enough in other languages to properly translate those, so if others would like to contribute some translations would be nice. Otherwise I would need to resort to using something like google translate.

    opened by topjor 3
  • BIC Validation Issue

    BIC Validation Issue

    I'm using the BIC validation and it works fine but I noticed that the BIC number DEUTDBBER is validated as valid, even though it seems to be an invalid BIC number: https://transferwise.com/gb/swift-codes/bic-swift-code-checker?code=DEUTDBBER&direction=receive Can you help?

    opened by hofmannsven 3
  • added empty_with validator

    added empty_with validator

    I've added an empty_with validator that checks if either the field under validation is empty or all of the other specified fields. With this validator you can make an either [this field] or [that field] validation.

    opened by willemo 3
  • How can I overwrite the messages?

    How can I overwrite the messages?

    Hi! The package works great for validating IBAN numbers! Thanks! However, is it possible to overwrite the message? setting 'iban' => 'De :attribute is geen geldig IBAN nummer', in resources/lang/en/validation.php doesn't work, and neither does passing a custom message to a validator? This does work for the built-in validators. Thanks!

    opened by PanMan 3
  • Broken

    Broken

    This one appears to have broken with the recent changes to the framework. I'm getting an error: Method [validateUsername] does not exist.

    The provider is in place, so the validator should be picked up.

    opened by syphernl 3
  • Validation rule does not exist

    Validation rule does not exist

    Since the 2.0.0-beta1 I'm having issues with my custom validation rules (see using extensions).

    I added the custom validation rule right inside my app service provider:

    Validator::extend('foo', '[email protected]');
    

    And I keep getting the error:

    Validation rule (Intervention\Validation\Rules\Foo) does not exist.

    Can you help?

    opened by hofmannsven 2
  • Iban Validation Issue

    Iban Validation Issue

    Hello,

    We are using the IBAN validation and noticed, that if we use 507008 as IBAN, it will return true instead of false. But since 507008 is not a valid IBAN it shouldn’t return true.

    opened by dennisoderwald 2
  • Fixed issue when checksum remainder is 0

    Fixed issue when checksum remainder is 0

    There was in issue with GTIN and EAN when the module remainder was zero. See Wikipediafor this special rule.

    "Subtracted from 10, that leaves a result from 1 to 10. A zero replaces a ten, so, in all cases, a single check digit results."

    Had to remove 0000000000000 === false from the tests, since technically it is now a valid GTIN/EAN. I also added a check that the value has to be numeric.

    opened by lasselehtinen 1
  • Package seems to cause translation errors in other packages

    Package seems to cause translation errors in other packages

    It seems that this package causes translation errors in Laravel Nova (see this issue https://github.com/laravel/nova-issues/issues/4192).

    By calling a translation in a constructor the incorrect locale is set for the App (https://github.com/laravel/nova-issues/issues/4192#issuecomment-1120249039)

    I think this is what is happening with this package in the file validation/src/Laravel/ValidationServiceProvider.php.

    From the boot() function $this->getErrorMessage() is called and this calls return $this->app['translator']->get('validation::validation.' . $rulename);. (https://github.com/Intervention/validation/blob/master/src/Laravel/ValidationServiceProvider.php#L48)

    When I comment this out or replace it with return ''; the translation errors in Laravel Nova disappear.

    I am using php8, laravel/framework 9.26.1, laravel/nova 4.13.0, intervention/validation 3.2.0

    opened by sanderbaas 1
Releases(2.4.1)
Owner
null
🔒 Laravel validation rule that checks if a password has been exposed in a data breach.

?? Laravel Password Exposed Validation Rule This package provides a Laravel validation rule that checks if a password has been exposed in a data breac

Jordan Hall 85 Apr 26, 2022
高性能的验证器组件(Validation),适用于 Hyperf 或 Laravel 框架,可获得数百倍的性能提升

验证器 简介 兼容 Hyperf/Laravel Validation 规则 部分场景可获得约 500 倍性能提升 验证器可多次复用不同数据,无状态设计 规则可全局复用 智能合并验证规则 安装 环境要求 PHP >= 8.0 mbstring 扩展 ctype 扩展 安装命令 composer re

KK集团 80 Dec 9, 2022
Extra validation rules for dealing with images in Laravel 5.

Image-Validator Extra validation rules for dealing with images in Laravel 5. NOTE: As of Laravel version 5.2, there are now built-in validation rules

Colin Viebrock 223 Jun 16, 2022
Laravel Validation Service

Laravel Validation Service Installation Add "prettus/laravel-repository": "1.1.*" to composer.json "prettus/laravel-validation": "1.1.*" Create a vali

Anderson Andrade 398 Nov 25, 2022
🔒 Laravel validation rule that checks if a password has been exposed in a data breach.

?? Laravel Password Exposed Validation Rule This package provides a Laravel validation rule that checks if a password has been exposed in a data breac

Jordan Hall 85 Apr 26, 2022
File uploads with validation and storage strategies

Upload This component simplifies file validation and uploading. Usage Assume a file is uploaded with this HTML form: <form method="POST" enctype="mult

Brandon Savage 1.7k Dec 27, 2022
Valitron is a simple, elegant, stand-alone validation library with NO dependencies

Valitron: Easy Validation That Doesn't Suck Valitron is a simple, minimal and elegant stand-alone validation library with NO dependencies. Valitron us

Vance Lucas 1.5k Dec 30, 2022
Lightweight and feature-rich PHP validation and filtering library. Support scene grouping, pre-filtering, array checking, custom validators, custom messages. 轻量且功能丰富的PHP验证、过滤库。支持场景分组,前置过滤,数组检查,自定义验证器,自定义消息。

PHP Validate 一个简洁小巧且功能完善的php验证、过滤库。 简单方便,支持添加自定义验证器 支持前置验证检查, 自定义如何判断非空 支持将规则按场景进行分组设置。或者部分验证 支持在进行验证前对值使用过滤器进行净化过滤内置过滤器 支持在进行验证前置处理和后置处理独立验证处理 支持自定义每

Inhere 246 Jan 5, 2023
Abstracts HTTP request input handling, providing an easy interface for data hydration and validation

Linio Input Linio Input is yet another component of the Linio Framework. It aims to abstract HTTP request input handling, allowing a seamless integrat

Linio 41 Dec 12, 2021
[READ-ONLY] Validation library from CakePHP. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp

CakePHP Validation Library The validation library in CakePHP provides features to build validators that can validate arbitrary arrays of data with eas

CakePHP 39 Oct 11, 2022
Light and extendable schema validation library

Light PHP validation library For everyone who uses MongoDB or other NoSQL solution and cares about what client sends to his/her database and looking f

Alexander Serkin 43 Sep 28, 2022
One Line Validation, For CodeIgniter 4

One Line Validation (OLV) is a package made for CodeIgniter 4 to provide a fast and single line validation experience ideal for CDN and/or API services consuming the Validation System from CodeIgniter 4.

AJ Meireles 2 Sep 21, 2021
Validation rules for Money and Currency

money-validation-laravel Validation rules for Money and Currency Installation composer require brokeyourbike/money-validation-laravel Usage Package us

Ivan Stasiuk 1 Oct 25, 2021
laminas-password-validator provides a validator for character-set based input validation.

laminas-password-validator laminas-password-validator provides a validator for character-set based input validation. Installation composer require pra

null 1 Mar 8, 2022
An extensible validation library for your data with sane defaults.

Hird Hirds, also known as housecarls, was a gathering of hirdmen, who functioned as the king's personal guards during the viking age and the early mid

Asko Nõmm 13 Apr 23, 2022
FyreValidation is a free, open-source validation library for PHP.

FyreValidation FyreValidation is a free, validation library for PHP. Table Of Contents Installation Validators Rules Error Messages Installation Using

Elusive 0 Jan 15, 2022
Laravel quickly creates a verification code tool similar to Google verification code

laravel-gridCaptcha Laravel quickly creates a verification code tool similar to Google verification code laravel 快速创建一个类似于 Google 点图验证码的本地验证码扩展 介绍 lar

delete DB Flee 14 Mar 1, 2022
Custom Laravel Validator for combined unique indexes

unique_with Validator Rule For Laravel This package contains a variant of the validateUnique rule for Laravel, that allows for validation of multi-col

Felix Kiss 383 Oct 18, 2022
Extension for the Laravel validation class

Intervention Validation Intervention Validation is an extension library for Laravel's own validation system. The package adds rules to validate data l

null 370 Dec 30, 2022
Actions: controller + auth + validation in one class

Actions: controller + auth + validation in one class This package provides only one class: an Action class that extends the FormRequest class we all k

edatta 2 Dec 15, 2022