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.
You can install this package quick and easy with Composer.
Require the package via Composer:
$ composer require intervention/validation
The Validation library is built to work with the Laravel Framework (>=5.5). It comes with a service provider, which will be discovered automatically and registers the validation rules into your installation.
use Intervention\Validation\Validator;
use Intervention\Validation\Rules\HexColor;
use Intervention\Validation\Exception\ValidationException;
// create validator (for HexColor)
$validator = new Validator(new HexColor);
// validate against given values
$valid = $validator->validate('#ccc'); // true
$valid = $validator->validate('www'); // false
// change the validation rule
$validator->setRule(new Domainname);
// now validate new rule domainname
$valid = $validator->validate('foo.com'); // true
$valid = $validator->validate('?'); // false
// validator can also throw exceptions on invalid data.
// just call assert() instead of validate().
try {
$validator->assert('foobar');
} catch (ValidationException $e) {
echo $e->getMessage();
}
use Intervention\Validation\Validator;
use Intervention\Validation\Rules\HexColor;
use Intervention\Validation\Exception\ValidationException;
// create validator statically
$valid = Validator::make(new HexColor)->validate('ccc'); // true
$valid = Validator::make(new HexColor)->validate('#www'); // false
// throw exceptions on invalid data instead of returning boolean
try {
Validator::make(new HexColor)->assert('www');
} catch (ValidationException $e) {
echo $e->getMessage();
}
use Intervention\Validation\Validator;
use Intervention\Validation\Rules\HexColor;
use Intervention\Validation\Exception\ValidationException;
// call validation rule directly via static method
$valid = Validator::isHexColor('#ccc'); // true
$valid = Validator::isHexColor('#www'); // false
// throw exceptions on invalid data
try {
Validator::assertHexColor('foo');
} catch (ValidationException $e) {
echo $e->getMessage();
}
The installed package provides additional validation rules
including their error messages.
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($request->all(), [
'color' => 'required|hexcolor',
'number' => 'iban',
]);
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.
The following validation rules are available.
Checks if given value is Base64 encoded.
Checks for a valid Business Identifier Code (BIC).
The given field must be a formated in Camel case.
Check if the value is a Classless Inter-Domain Routing notation (CIDR).
The given field must be a valid creditcard number.
The given field must be a well formed domainname.
The field under validation must be a valid hexadecimal color code.
The field under validation must be free of any html code.
Checks for a valid International Bank Account Number (IBAN).
The given field must be a International Mobile Equipment Identity (IMEI).
The field under validation must be a valid International Standard Book Number (ISBN).
Checks for a valid International Securities Identification Number (ISIN).
Checks for a valid International Standard Serial Number (ISSN).
The given value must be a in format of a JSON Web Token.
The given value must be formated in Kebab case.
The given value must be all lower case letters.
The given value must verify against its included Luhn algorithm check digit.
The field under validation must be a media access control address (MAC address).
The given field must be a valid version numbers using Semantic Versioning.
The field under validation must be a user- and SEO-friendly short text.
The field under validation must formated as Snake case text.
The field under validation must formated in Title case.
The field under validation must be all upper case.
The field under validation must be a valid username with a minimum of 3 characters and maximum of 20 characters. 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.
Intervention Validation is licensed under the MIT License.