PHPackages                             anekdotes/validator - 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. [Testing &amp; Quality](/categories/testing)
4. /
5. anekdotes/validator

ActiveLibrary[Testing &amp; Quality](/categories/testing)

anekdotes/validator
===================

A validator class to validate inputs

2.0.0(4y ago)06331MITPHPPHP &gt;=7.4.0

Since Mar 17Pushed 4y ago1 watchersCompare

[ Source](https://github.com/anekdotes/validator)[ Packagist](https://packagist.org/packages/anekdotes/validator)[ RSS](/packages/anekdotes-validator/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependencies (1)Versions (9)Used By (0)

Anekdotes Validator
===================

[](#anekdotes-validator)

[![Latest Stable Version](https://camo.githubusercontent.com/de57cc408d64f10261d8226c4e52b2873e70fcf96cdcf1bee46f24c7321d79ca/68747470733a2f2f706f7365722e707567782e6f72672f616e656b646f7465732f76616c696461746f722f762f737461626c65)](https://packagist.org/packages/anekdotes/validator)[![Build Status](https://camo.githubusercontent.com/86a4d94088ca690da7aa708182b50b5ed24f88ba280df4b202708d37d8decec5/68747470733a2f2f7472617669732d63692e6f72672f616e656b646f7465732f76616c696461746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/anekdotes/validator)[![codecov.io](https://camo.githubusercontent.com/07cd9a8f9f33084ec4469cbe96f1aa67763812427fb64ddeaccc96e3ad30b914/68747470733a2f2f636f6465636f762e696f2f6769746875622f616e656b646f7465732f76616c696461746f722f636f7665726167652e7376673f6272616e63683d6d6173746572)](https://codecov.io/github/anekdotes/validator?branch=master)[![StyleCI](https://camo.githubusercontent.com/b334dc89615146e2b7403b6acdb2bacd1f32d4cbf825ceaffcb9fce7ad887d76/68747470733a2f2f7374796c6563692e696f2f7265706f732f35333639383636382f736869656c643f7374796c653d666c6174)](https://styleci.io/repos/53698668)[![License](https://camo.githubusercontent.com/e8e5d0a0e769e28b92f0d57a93dfb8720f1c08856de41d39b7febfb45a8a2940/68747470733a2f2f706f7365722e707567782e6f72672f616e656b646f7465732f76616c696461746f722f6c6963656e7365)](https://packagist.org/packages/anekdotes/validator)[![Total Downloads](https://camo.githubusercontent.com/2db1774150c020227cdd5d23ed549ac5e034e254d08f9a77fabb1535df36a46e/68747470733a2f2f706f7365722e707567782e6f72672f616e656b646f7465732f76616c696461746f722f646f776e6c6f616473)](https://packagist.org/packages/anekdotes/validator)

---

A validator class to validate input data against validation types.

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

[](#installation)

Install via composer in your project :

```
composer require anekdotes/validator

```

Basic usage
-----------

[](#basic-usage)

Prepare an input array to validate and a rules array to validate against

```
  $input = [
    "formName" => $_POST["formName"],
    "formEmail" => $_POST["formEmail"],
    "otherData" => "Bob"
  ];

  $rules = [
    "formName" => ["required"],
    "formEmail" => ["required", "email"]
  ]
```

Then, instantiate the validator with the rules and use its status to follow-up with code.

```
// initiate the Validator with inputs and rules
use Anekdotes\Validator;
function doSomething(){
    $validator = Validator::make($inputs, $rules);

    // test if validator pass all the tests
    if($validator->fail()) {
        //Log something maybe?
        //Display a message maybe?
        return false;
	  }

    //Proceed with the data
    //Store it in the db?

}
```

Rule Types
----------

[](#rule-types)

#### required

[](#required)

Check if the input is empty

```
  $rules = ["inputField" => "required"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "" ];
  $inputB = ["otherInput" => "Whatever" ];

  //The following inputs would validate as a success
  $inputC = ["inputField" => "Something" ];
  $inputD = ["inputField" => "Stuff" , "otherInput" => "anythingElse"];
```

#### requiredIf

[](#requiredif)

Check if the input is empty, but only if an other input's value equals a specific value

```
  $rules = ["inputField" => "requiredIf:otherInput,otherInputsValue"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "" , "otherInput" => "otherInputsValue"];

  //The following inputs would validate as a success
  $inputB = ["inputField" => "" , "otherInput" => "anythingElse"];
  $inputC = ["inputField" => ""];
  $inputD = ["inputField" => "Data" , "otherInput" => "otherInputsValue"];
  $inputE = ["inputField" => "Data" , "otherInput" => ""];
```

#### requiredWith

[](#requiredwith)

Check if the input is empty if an other input is not empty

```
  $rules = ["inputField" => "requiredWith:otherInput"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "" , "otherInput" => "otherInputsValue"];
  $inputB = ["inputField" => "" , "otherInput" => "anythingElse"];

  //The following inputs would validate as a success
  $inputC = ["inputField" => ""];
  $inputD = ["inputField" => "Data" , "otherInput" => "otherInputsValue"];
  $inputE = ["inputField" => "Data" , "otherInput" => ""];
```

#### requiredWithout

[](#requiredwithout)

Check if the input is empty if an other input is empty

```
  $rules = ["inputField" => "requiredWithout:otherInput"];

  //The following inputs would validate as a failure
  $inputC = ["inputField" => ""];

  //The following inputs would validate as a success
  $inputA = ["inputField" => "" , "otherInput" => "otherInputsValue"];
  $inputB = ["inputField" => "" , "otherInput" => "anythingElse"];
  $inputD = ["inputField" => "Data" , "otherInput" => "otherInputsValue"];
  $inputE = ["inputField" => "Data" , "otherInput" => ""];
```

#### integer

[](#integer)

Check if the input is an integer

```
  $rules = ["inputField" => "integer"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "a"];
  $inputB = ["inputField" => "12"];
  $inputC = ["inputField" => 12.3];

  //The following inputs would validate as a success
  $inputD = ["inputField" => 1];
```

#### numeric

[](#numeric)

Check if the input is numeric

```
  $rules = ["inputField" => "numeric"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "a"];

  //The following inputs would validate as a success
  $inputB = ["inputField" => "12"];
  $inputC = ["inputField" => 12.3];
  $inputD = ["inputField" => 1];
```

#### date

[](#date)

Check if the input is a date

```
  $rules = ["inputField" => "date"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "a"];

  //The following inputs would validate as a success
  $inputB = ["inputField" => "1-1-2000"];
```

#### different

[](#different)

Check if the input is different from follow-up values

```
  $rules = ["inputField" => "different:Git,Hub"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "Git"];
  $inputB = ["inputField" => "Hub"];

  //The following inputs would validate as a success
  $inputC = ["inputField" => "git"];
  $inputD = ["inputField" => "toast"];
  $inputE = ["inputField" => "GitHub"];
```

#### email

[](#email)

Check if the input matches an email address

```
  $rules = ["inputField" => "email"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "test"];
  $inputB = ["inputField" => "test@test"];

  //The following inputs would validate as a success
  $inputC = ["inputField" => "gmail@me.com"];
  $inputD = ["inputField" => "test@test.test"];
```

#### postalCode

[](#postalcode)

Check if the input matches a canadian postal code

```
  $rules = ["inputField" => "postalCode"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "test"];
  $inputB = ["inputField" => "123456"];

  //The following inputs would validate as a success
  $inputC = ["inputField" => "J4R 2L6"];
  $inputD = ["inputField" => "A1A1A1"];
```

#### phoneNumber

[](#phonenumber)

Check if the input matches an american phone number

```
  $rules = ["inputField" => "phoneNumber"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "test"];
  $inputB = ["inputField" => "123456"];

  //The following inputs would validate as a success
  $inputC = ["inputField" => "4507482822"];
  $inputD = ["inputField" => "1-800-123-4567"];
  $inputE = ["inputField" => "1 (800) 123-4567"];
```

#### between

[](#between)

Check if the input is between a minimum and a maximum

Available types of input:

- String : Tests the string's character count
- Number : Tests the number's value
- Files : Tests the file's size in KiloBytes

```
  $rules = ["inputField" => "between:3,5"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "tested"];
  $inputB = ["inputField" => 6];

  //The following inputs would validate as a success
  $inputC = ["inputField" => 4];
  $inputD = ["inputField" => "test"];
  $inputE = ["inputField" => "5"];
```

#### minimum

[](#minimum)

Check if the input is above a minimum

Available types of input:

- String : Tests the string's character count
- Number : Tests the number's value
- Files : Tests the file's size in KiloBytes

```
  $rules = ["inputField" => "minimum:3"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "ta"];
  $inputB = ["inputField" => 2];

  //The following inputs would validate as a success
  $inputC = ["inputField" => 4];
  $inputD = ["inputField" => "est"];
  $inputE = ["inputField" => "5"];
```

#### maximum

[](#maximum)

Check if the input is under a maximum

Available types of input:

- String : Tests the string's character count
- Number : Tests the number's value
- Files : Tests the file's size in KiloBytes

```
  $rules = ["inputField" => "maximum:3"];

  //The following inputs would validate as a failure
  $inputC = ["inputField" => 4];
  $inputD = ["inputField" => "test"];
  $inputE = ["inputField" => "5"];

  //The following inputs would validate as a success
  $inputA = ["inputField" => "ta"];
  $inputB = ["inputField" => 2];
```

#### size

[](#size)

Check if the input has the exact size

Available types of input:

- String : Tests the string's character count
- Number : Tests the number's value
- Files : Tests the file's size in KiloBytes

```
  $rules = ["inputField" => "size:3"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "ta"];
  $inputB = ["inputField" => 232];

  //The following inputs would validate as a success
  $inputC = ["inputField" => 3];
  $inputD = ["inputField" => "abs"];
  $inputE = ["inputField" => "3"];
```

#### length

[](#length)

Check if the input has the exact string length provided (works the samne way as size, but only for strings)

```
  $rules = ["inputField" => "length:3"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "ta"];
  $inputB = ["inputField" => "3"];

  //The following inputs would validate as a success
  $inputC = ["inputField" => "125"];
  $inputD = ["inputField" => "abs"];
```

#### url

[](#url)

Check if the input matches a URL

```
  $rules = ["inputField" => "url"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "ta"];

  //The following inputs would validate as a success
  $inputB = ["inputField" => "http://www.anekdotes.com"];
  $inputC = ["inputField" => "www.google.ca"];
```

#### validUrl

[](#validurl)

Check if the input matches an existing URL, based on PHP's DNS Check

```
  $rules = ["inputField" => "validUrl"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "ta"];

  //The following inputs would validate as a success
  $inputB = ["inputField" => "http://www.anekdotes.com"];
  $inputC = ["inputField" => "www.google.ca"];
```

#### same

[](#same)

Check if the input had the same value as another input (useful for password checks)

```
  $rules = ["inputField" => "same:otherInput"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "" , "otherInput" => "otherInputsValue"];
  $inputB = ["inputField" => "maybe" , "otherInput" => "anythingElse"];

  //The following inputs would validate as a success
  $inputC = ["inputField" => "qwerty123456", "otherInput" => "qwerty123456"];
  $inputE = ["inputField" => "Data" , "otherInput" => "Data"];
```

#### alpha

[](#alpha)

Check if the input contains only alphabetic characters

```
  $rules = ["inputField" => "alpha"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "a12b"];
  $inputB = ["inputField" => "hi there"];
  $inputC = ["inputField" => "marks-the-spot"];

  //The following inputs would validate as a success
  $inputD = ["inputField" => "anekdotes"];
```

#### alpha\_num

[](#alpha_num)

Check if the input contains only alphabetic and numeric characters

```
  $rules = ["inputField" => "alpha_num"];

  //The following inputs would validate as a failure
  $inputB = ["inputField" => "hi there"];
  $inputC = ["inputField" => "marks-the-spot"];

  //The following inputs would validate as a success
  $inputA = ["inputField" => "a12b"];
  $inputD = ["inputField" => "anekdotes"];
```

#### alpha\_dash

[](#alpha_dash)

Check if the input contains only alphabetic,numeric and dashes characters

```
  $rules = ["inputField" => "alpha_num"];

  //The following inputs would validate as a failure
  $inputB = ["inputField" => "hi there"];

  //The following inputs would validate as a success
  $inputA = ["inputField" => "a12b"];
  $inputC = ["inputField" => "marks-the-spot"];
  $inputD = ["inputField" => "anekdotes"];
```

#### before

[](#before)

Check if the input is a date older than the provided :date

```
  $rules = ["inputField" => "before:2016-12-31"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "hi there"];
  $inputB = ["inputField" => "2017-01-01"];

  //The following inputs would validate as a success
  $inputC = ["inputField" => "2015-02-28"];
```

#### after

[](#after)

Check if the input if a date younger than the provided :date

```
  $rules = ["inputField" => "after:2016-12-31"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "hi there"];
  $inputC = ["inputField" => "2015-02-28"];

  //The following inputs would validate as a success
  $inputB = ["inputField" => "2017-01-01"];
```

#### digits

[](#digits)

Check if the input is an integer that contains exactly x digits

```
  $rules = ["inputField" => "digits:4"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "1234567890"];
  $inputB = ["inputField" => "1a28"];

  //The following inputs would validate as a success
  $inputC = ["inputField" => "2017"];
```

#### digits\_between

[](#digits_between)

Check if the input

Check if the input is an integer that contains between x and y digits

```
  $rules = ["inputField" => "digits_between:4,6"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "1234567890"];
  $inputB = ["inputField" => "1a28"];

  //The following inputs would validate as a success
  $inputC = ["inputField" => "2017"];
  $inputD = ["inputField" => "201712"];
```

#### confirmed

[](#confirmed)

Checks that if a confirmation field has the same value that the other one (Perfect for password or email checks)

```
  $rules = ["inputField" => "confirmed"];

  //The following inputs would validate as a failure
  $inputA = ["inputField" => "qwerty123" , "inputField_confirmation" => "123456"];
  $inputB = ["inputField" => "" , "inputField_confirmation" => "123456"];
  $inputC = ["inputField" => "123456"];

  //The following inputs would validate as a success
  $inputD = ["inputField" => "123456" , "inputField_confirmation" => "123456"];
  $inputE = ["inputField" => "test@test.test" , "inputField_confirmation" => "test@test.test"];
```

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 61.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~346 days

Recently: every ~518 days

Total

7

Last Release

1680d ago

Major Versions

v1.1.0 → 2.0.02021-11-24

PHP version history (2 changes)v1.0.0PHP &gt;=5.3.0

2.0.0PHP &gt;=7.4.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/17788771?v=4)[Anekdotes](/maintainers/anekdotes)[@anekdotes](https://github.com/anekdotes)

---

Top Contributors

[![punkaid](https://avatars.githubusercontent.com/u/4768849?v=4)](https://github.com/punkaid "punkaid (44 commits)")[![Grasseh](https://avatars.githubusercontent.com/u/2159610?v=4)](https://github.com/Grasseh "Grasseh (28 commits)")

---

Tags

testvalidationform

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/anekdotes-validator/health.svg)

```
[![Health](https://phpackages.com/badges/anekdotes-validator/health.svg)](https://phpackages.com/packages/anekdotes-validator)
```

###  Alternatives

[mockery/mockery

Mockery is a simple yet flexible PHP mock object framework

10.7k526.2M26.3k](/packages/mockery-mockery)[hamcrest/hamcrest-php

This is the PHP port of Hamcrest Matchers

7.0k513.4M117](/packages/hamcrest-hamcrest-php)[beberlei/assert

Thin assertion library for input validation in business models.

2.4k100.2M616](/packages/beberlei-assert)[nelmio/alice

Expressive fixtures generator

2.5k44.6M158](/packages/nelmio-alice)[zenstruck/foundry

A model factory library for creating expressive, auto-completable, on-demand dev/test fixtures with Symfony and Doctrine.

79513.1M135](/packages/zenstruck-foundry)[php-mock/php-mock

PHP-Mock can mock built-in PHP functions (e.g. time()). PHP-Mock relies on PHP's namespace fallback policy. No further extension is needed.

37419.9M121](/packages/php-mock-php-mock)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
