PHPackages                             f9webltd/laravel-validation-rules - 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. f9webltd/laravel-validation-rules

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

f9webltd/laravel-validation-rules
=================================

A collection of useful Laravel validation rules

1.1.1(5y ago)152511MITPHPPHP ^7.2CI failing

Since Jun 4Pushed 5y ago1 watchersCompare

[ Source](https://github.com/f9webltd/laravel-validation-rules)[ Packagist](https://packagist.org/packages/f9webltd/laravel-validation-rules)[ Docs](https://github.com/f9webltd/laravel-validation-rules)[ RSS](/packages/f9webltd-laravel-validation-rules/feed)WikiDiscussions master Synced today

READMEChangelog (3)Dependencies (4)Versions (4)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/cf5293269b42c68e0c6b5e02bf401411afe0dc20f2232877f9277559a084a13a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f66397765626c74642f6c61726176656c2d76616c69646174696f6e2d72756c6573)](https://packagist.org/packages/f9webltd/laravel-validation-rules)![Scrutinizer coverage (GitHub/BitBucket)](https://camo.githubusercontent.com/50edc40211a1cfd9e447d6954e964b862c7f705a623a3a4b8aaed523f76463a5/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f66397765626c74642f6c61726176656c2d76616c69646174696f6e2d72756c6573)![Scrutinizer code quality (GitHub/Bitbucket)](https://camo.githubusercontent.com/29e6fd26aff23502648c1eb572d160ea0ed19482487e440534ff6dc932b4ba2c/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f7175616c6974792f672f66397765626c74642f6c61726176656c2d76616c69646174696f6e2d72756c6573)[![Build Status](https://camo.githubusercontent.com/6da191c5fa605c49d802f9230abfebad69ae39ee5941b123c88c857e6d4c8a15/68747470733a2f2f7472617669732d63692e6f72672f66397765626c74642f6c61726176656c2d76616c69646174696f6e2d72756c65732e737667)](https://travis-ci.org/f9webltd/laravel-validation-rules)[![StyleCI Status](https://camo.githubusercontent.com/637d15eda25fd231d92652657c507b9562c06a29f636914ab4434f56fa78e42d/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3236363939373638392f736869656c64)](https://github.styleci.io/repos/266997689)[![License](https://camo.githubusercontent.com/11207e6358d96e19871a43c73f3dd6d5f6917d94dd7552334aeff3eac8d84684/68747470733a2f2f706f7365722e707567782e6f72672f66397765626c74642f6c61726176656c2d76616c69646174696f6e2d72756c65732f6c6963656e7365)](https://packagist.org/packages/f9webltd/laravel-validation-rules)

Useful Laravel Validation Rules
===============================

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

A collection of useful Laravel validation rules.

Requirements
------------

[](#requirements)

PHP &gt;= 7.2, Laravel `>=5.8 | 6.x | 7.x | 8.x`.

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

[](#installation)

```
composer require f9webltd/laravel-validation-rules
```

To publish the package validation message translations:

```
php artisan vendor:publish --provider="F9Web\ValidationRules\ValidationRulesServiceProvider"
```

Published translations are available at `resources/lang/vendor/f9web-validation-rules/messages.php`.

Usage
-----

[](#usage)

As discussed in the official [Laravel documentation](https://laravel.com/docs/master/validation#using-rule-objects), import the required rule whenever required:

```
use F9Web\ValidationRules\Rules\TitleCase;

// ...

$request->validate([
    'team' => ['required', new TitleCase()],
]);
```

Alternatively use the rule directly with a [Laravel form request object](https://laravel.com/docs/7.x/validation#form-request-validation)

Available rules
---------------

[](#available-rules)

- [`Base64EncodedString`](#base64encodedstring)
- [`Coordinate`](#coordinate)
- [`DomainRestrictedEmail`](#domainrestrictedemail)
- [`ExcludesHtml`](#excludeshtml)
- [`HexColourCode`](#hexcolourcode)
- [`Honorific`](#honorific)
- [`IncludesHtml`](#includeshtml)
- [`NoWhitespace`](#nowhitespace)
- [`NumberParity`](#numberparity)
- [`StringContains`](#stringcontains)
- [`StrongPassword`](#strongpassword)
- [`TitleCase`](#titlecase)
- [`UKMobilePhone`](#ukmobilephone)
- [`Uppercase`](#uppercase)

### `Base64EncodedString`

[](#base64encodedstring)

Ensure the passed attribute is a valid base 64 encoded string.

### `Coordinate`

[](#coordinate)

Ensure the passed attribute is a valid comma separated Latitude and Longitude string. For example: `51.507877,-0.087732`.

### `DomainRestrictedEmail`

[](#domainrestrictedemail)

Ensure the passed email in question is part of the provided whitelist of domains.

For instance, to ensure the given email domain is `f9web.co.uk` or `laravel.com`:

```
use F9Web\ValidationRules\Rules\DomainRestrictedEmail;

// ...

$request->validate([
    'email' => [
        'required',
        (new DomainRestrictedEmail())->validDomains([
            'f9web.co.uk',
            'laravel.com',
        ]),
    ],
]);
```

The validation message will include the list of whitelisted domains based upon the provided configuration.

### `ExcludesHtml`

[](#excludeshtml)

Ensure the passed attribute does not contain HTML.

### `HexColourCode`

[](#hexcolourcode)

Ensure the passed attribute is a valid hex colour code (three of six characters in length), optionally validating the presence of the `#` prefix.

Minimum usage example to validate a short length code with the prefix i.e. `#fff`:

```
use F9Web\ValidationRules\Rules\HexColourCode;

(new HexColourCode());
```

Extended usage example to validate a long length code , omitting prefix i.e. `cc0000`:

```
use F9Web\ValidationRules\Rules\HexColourCode;

(new HexColourCode())->withoutPrefix()->longFormat();
```

### `Honorific`

[](#honorific)

Ensure the passed attribute is a valid honorific, omitting appended dots. The list of valid honorifics is available [here](src/Support/Honorifics.php).

### `IncludesHtml`

[](#includeshtml)

Ensure the passed attribute contains HTML.

### `NoWhitespace`

[](#nowhitespace)

Ensure the passed attribute contains no whitespace.

### `NumberParity`

[](#numberparity)

Validate the number parity.

An odd number:

```
use F9Web\ValidationRules\Rules\NumberParity;

// ...

$request->validate([
    'amount' => [
        'required',
        (new NumberParity())->odd(),
    ],
]);
```

An even number:

```
use F9Web\ValidationRules\Rules\NumberParity;

// ...

$request->validate([
    'amount' => [
        'required',
        (new NumberParity())->even(),
    ],
]);
```

### `StringContains`

[](#stringcontains)

Ensure the given attribute contains the provided strings.

Minimum usage example to ensure the attribute in question contains the string `php` or `laravel`:

```
use F9Web\ValidationRules\Rules\StringContains;

// ...

$request->validate([
    'description' => [
        'required',
        (new StringContains())->phrases([
            'laravel',
            'php',
        ]),
    ],
]);
```

Optionally force the string to contain *all* provided phrases:

```
use F9Web\ValidationRules\Rules\StringContains;

// ...

$request->validate([
    'description' => [
        'required',
        (new StringContains())->phrases([
            'laravel',
            'php',
        ])->strictly(),
    ],
]);
```

The validation message will include the list phrases based upon the provided configuration.

### `StrongPassword`

[](#strongpassword)

Ensure the given attribute matches the provided conditions.

Minimum usage example to ensure the attribute:

- is a minimum of eight characters in length
- contains upper and lowercase characters
- contains at least one number

```
use F9Web\ValidationRules\Rules\StrongPassword;

// ...

$request->validate([
    'password' => [
        'required',
        (new StrongPassword()),
    ],
]);
```

Additional methods are available.

```
use F9Web\ValidationRules\Rules\StrongPassword;

// ...

$request->validate([
    'password' => [
        'required',
        (new StrongPassword())
            ->forceUppercaseCharacters()
            ->forceLowercaseCharacters(false)
            ->forceNumbers()
            ->forceSpecialCharacters()
            // ->withSpecialCharacters('£$*%^'),
    ],
]);
```

The default special characters are `!@#$%^&*()\-_=+{};:,
