PHPackages                             nilportugues/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. [Debugging &amp; Profiling](/categories/debugging)
4. /
5. nilportugues/validator

ActiveLibrary[Debugging &amp; Profiling](/categories/debugging)

nilportugues/validator
======================

An efficient data validator for PHP

3.0.3(10y ago)121.5k2[1 issues](https://github.com/nilportugues/validator/issues)MITPHP

Since Sep 26Pushed 10y ago2 watchersCompare

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

READMEChangelog (7)Dependencies (4)Versions (9)Used By (0)

Input Validator
===============

[](#input-validator)

[![Build Status](https://camo.githubusercontent.com/bd165be123b57d2aa4613facbfae2e53898a57ae23ff43e97649509cddcf801b/68747470733a2f2f7472617669732d63692e6f72672f6e696c706f727475677565732f7068702d76616c696461746f722e737667)](https://travis-ci.org/nilportugues/php-validator) [![Coverage Status](https://camo.githubusercontent.com/1f8f7248b6eb826e6a2b96fb269850942e4bc30ea8f67fbb617701bc753edade/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6e696c706f727475677565732f76616c696461746f722e737667)](https://coveralls.io/r/nilportugues/validator?branch=master) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/53d29513fc583975f9792219f6b6093915731fed2b46c6f61b629c220c04cd7f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e696c706f727475677565732f76616c696461746f722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/nilportugues/validator/?branch=master) [![SensioLabsInsight](https://camo.githubusercontent.com/d37b2abc69ff0dee6105b817a05a263add548e692b8e817781ca7cd22cf5512a/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f39666262343930302d373062392d343966362d623764352d6139663536306364623033362f6d696e692e706e67)](https://insight.sensiolabs.com/projects/9fbb4900-70b9-49f6-b7d5-a9f560cdb036) [![Latest Stable Version](https://camo.githubusercontent.com/0e70698572be9b2382c932dea9d3873d37f1d16fdefb95f9ba1380b5b42f7bb9/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f76616c696461746f722f762f737461626c65)](https://packagist.org/packages/nilportugues/validator) [![Total Downloads](https://camo.githubusercontent.com/94cbf43827c6c8f035df30a6f3b121025a78925d775d0c50ecf89489176483d2/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f76616c696461746f722f646f776e6c6f616473)](https://packagist.org/packages/nilportugues/validator) [![License](https://camo.githubusercontent.com/1767cd95e571050bbde3ef52e271eeca6302a6075c7c6963aa55911425e454ea/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f76616c696461746f722f6c6963656e7365)](https://packagist.org/packages/nilportugues/validator)[![Donate](https://camo.githubusercontent.com/7b6de155df30b37b25eb5fec52f9213680c3dbf067dfb7d7e2850ac4096c7d05/68747470733a2f2f7777772e70617970616c6f626a656374732e636f6d2f656e5f55532f692f62746e2f62746e5f646f6e6174655f534d2e676966)](https://paypal.me/nilportugues)

Validation is a very common task in web applications. Data entered in forms needs to be validated. Data also needs to be validated before it is written into a database or passed to a web service.

NilPortugues\\Validator is a simple, powerful and elegant stand-alone validation library with no external dependencies.

- [1. Installation](#block1)
- [2. Usage](#block2)
    - [2.1. Validation made for everyone](#block2.1)
        - [2.1.1. Instantiation of Validator class](#block2.1.1)
        - [2.1.2. Factory using ValidatorFactory::create](#block2.1.2)
        - [2.1.3. Extending from the BaseValidator](#block2.1.3)
    - [2.2. Stop on first error](#block2.2)
- [3. Validation Message Translation](#block3)
- [4. Methods](#block4)
    - [4.1 isString | string](#block4.1)
        - \[4.1.1. isAlphanumeric | alphanumeric\] (#block4.1.1)
            - [4.1.1.1. Instantiation of Validator class](#block4.1.1.1)
            - [4.1.1.2. Factory using Validator::create](#block4.1.1.2)
            - [4.1.1.3. Extending from the BaseValidator](#block4.1.1.3)
        - \[4.1.2. isAlpha | alpha\] (#block4.1.2)
        - \[4.1.3. isBetween | between\] (#block4.1.3)
        - \[4.1.4. isCharset | charset\] (#block4.1.4)
        - \[4.1.5. isAllConsonants | all\_consonants\] (#block4.1.5)
        - \[4.1.6. contains\] (#block4.1.6)
        - \[4.1.7. isControlCharacters | control\_characters\] (#block4.1.7)
        - \[4.1.8. isDigit | digit\] (#block4.1.8)
        - \[4.1.9. endsWith | ends\_with\] (#block4.1.9)
        - \[4.1.10. equals\] (#block4.1.10)
        - \[4.1.11. in\] (#block4.1.11)
        - \[4.1.12. hasGraphicalCharsOnly | has\_graphical\_chars\_only\] (#block4.1.12)
        - \[4.1.13. hasLength | has\_length\] (#block4.1.13)
        - \[4.1.14. isLowercase | lowercase\] (#block4.1.14)
        - \[4.1.15. notEmpty | not\_empty\] (#block4.1.15)
        - \[4.1.16. noWhitespace | no\_whitespace\] (#block4.1.16)
        - \[4.1.17. hasPrintableCharsOnly | has\_printable\_chars\_only\] (#block4.1.17)
        - \[4.1.18. isPunctuation | punctuation\] (#block4.1.18)
        - \[4.1.19. matchesRegex | matches\_regex\] (#block4.1.19)
        - \[4.1.20. isSlug | slug\] (#block4.1.20)
        - \[4.1.21. isSpace | space\] (#block4.1.21)
        - \[4.1.22. startsWith | starts\_with\] (#block4.1.22)
        - \[4.1.23. isUppercase | uppercase\] (#block4.1.23)
        - \[4.1.24. isVersion | version\] (#block4.1.24)
        - \[4.1.25. isVowel | vowel\] (#block4.1.25)
        - \[4.1.26. isHexDigit | hex\_digit\] (#block4.1.26)
        - \[4.1.27. hasLowercase | has\_lowercase\] (#block4.1.27)
        - \[4.1.28. hasUppercase | has\_uppercase\] (#block4.1.28)
        - \[4.1.29. hasNumeric | has\_numeric\] (#block4.1.29)
        - \[4.1.30. hasSpecialCharacters | has\_special\_characters\] (#block4.1.30)
        - \[4.1.31. isEmail | email\] (#block4.1.31)
        - \[4.1.32. isUrl | url\] (#block4.1.32)
        - \[4.1.33. isUUID | uuid\] (#block4.1.33)
    - [4.2 isInteger | integer and isFloat | float](#block4.2)
        - \[4.2.1. isNotZero | not\_zero \] (#block4.2.1)
        - \[4.2.2. isPositive | positive\] (#block4.2.2)
        - \[4.2.2. isPositiveOrZero | positive\_or\_zero\] (#block4.2.3)
        - \[4.2.4. isNegative | negative\] (#block4.2.4)
        - \[4.2.5. isNegativeOrZero | negative\_or\_zero\] (#block4.2.5)
        - \[4.2.6. isBetween | between\] (#block4.2.6)
        - \[4.2.7. isOdd | odd\] (#block4.2.7)
        - \[4.2.8. isEven | even\] (#block4.2.8)
        - \[4.2.9. isMultiple | multiple\] (#block4.2.9)
    - [4.3 isObject | object](#block4.3)
        - \[4.3.1. isInstanceOf | instance\_of\] (#block4.3.1)
        - \[4.3.2. hasProperty | has\_property\] (#block4.3.2)
        - \[4.3.3. hasMethod | has\_method\] (#block4.3.3)
        - \[4.3.4. hasParentClass | has\_parent\_class\] (#block4.3.4)
        - \[4.3.5. isChildOf | child\_of\] (#block4.3.5)
        - \[4.3.6. inheritsFrom | inherits\_from\] (#block4.3.6)
        - \[4.3.7. hasInterface | has\_interface\] (#block4.3.7)
    - [4.4 isDateTime | date\_time](#block4.4)
        - \[4.4.1. isAfter | after\] (#block4.4.1)
        - \[4.4.2. isBefore | before\] (#block4.4.2)
        - \[4.4.3. isBetween | between\] (#block4.4.3)
        - \[4.4.4. isWeekend | weekend\] (#block4.4.4)
        - \[4.4.5. isWeekday | weekday\] (#block4.4.5)
        - \[4.4.6. isMonday | monday\] (#block4.4.6)
        - \[4.4.7. isTuesday | tuesday\] (#block4.4.7)
        - \[4.4.8. isWednesday | wednesday\] (#block4.4.8)
        - \[4.4.9. isThursday | thursday\] (#block4.4.9)
        - \[4.4.10. isFriday | friday\] (#block4.4.10)
        - \[4.4.11. isSaturday | saturday\] (#block4.4.11)
        - \[4.4.12. isSunday | sunday\] (#block4.4.12)
        - \[4.4.13. isToday | today\] (#block4.4.13)
        - \[4.4.14. isYesterday | yesterday\] (#block4.4.14)
        - \[4.4.15. isTomorrow | tomorrow\] (#block4.4.15)
        - \[4.4.16. isLeapYear | leap\_year\] (#block4.4.16)
        - \[4.4.17. isMorning | morning\] (#block4.4.17)
        - \[4.4.18. isAftenoon | aftenoon\] (#block4.4.18)
        - \[4.4.19. isEvening | evening\] (#block4.4.19)
        - \[4.4.20. isNight | night\] (#block4.4.20)
    - [4.5 isArray | array](#block4.5)
        - \[4.5.1. each\] (#block4.5.1)
        - \[4.5.2. hasKeyFormat | has\_key\_format\] (#block4.5.2)
        - \[4.5.3. endsWith | ends\_with\] (#block4.5.3)
        - \[4.5.4. contains\] (#block4.5.4)
        - \[4.5.5. hasKey | has\_key\] (#block4.5.5)
        - \[4.5.6. hasLength | has\_length\] (#block4.5.6)
        - \[4.5.7. isNotEmpty | not\_empty\] (#block4.5.7)
        - \[4.5.8. startsWith | starts\_with\] (#block4.5.8)
    - [4.6 isFileUpload | file\_upload](#block4.6)
        - \[4.6.1. isBetween | between\] (#block4.6.1)
        - \[4.6.2. isMimeType | mime\_type\] (#block4.6.2)
        - \[4.6.3. hasFileNameFormat | has\_file\_name\_format\] (#block4.6.3)
        - \[4.6.4. hasValidUploadDirectory | has\_valid\_upload\_directory\] (#block4.6.4)
        - \[4.6.5. notOverwritingExistingFile | not\_overwriting\_existing\_file\] (#block4.6.5)
        - \[4.6.6. hasLength | has\_length\] (#block4.6.6)
        - \[4.6.7. isImage | image\] (#block4.6.7)
- [4. Quality Code](#block4)
- [5. Author](#block5)
- [6. License](#block6)

1. Installation [↑](#index_block)
=================================

[](#1-installation-)

The recommended way to install the Input Validator is through [Composer](http://getcomposer.org). Run the following command to install it:

```
php composer.phar require nilportugues/validator
```

2. Usage [↑](#index_block)
==========================

[](#2-usage-)

The Validator interface is 100% human-friendly and readable. By default, it supports full validation and partial validation (stop when first error occurs).

### 2.1. Validation made for everyone [↑](#index_block)

[](#21-validation-made-for-everyone-)

When writing validator input data it is expected to be match a set of rules. If one or more of these rules fail, a collection of errors is returned. This is the default behaviour for `validate($input)`.

NilPortugues\\Validator supports up to 3 different styles to write validators: Instantiation, Factory or as a Class. Here's how you would validate an input `age` for these 3 styles:

#### 2.1.1. Instantiation of Validator class

[](#211-instantiation-of-validator-class)

```
use \NilPortugues\Validator\Validator;

$ageValidator = Validator::create()->isInteger('age');
$result = $ageValidator->isPositive()->isBetween(18, 100, true)->validate(28);
$errors = $ageValidator->getErrors();
```

#### 2.1.2. Factory using ValidatorFactory::create

[](#212-factory-using-validatorfactorycreate)

Using the Validator as a factory will provide a Validator object each time the `::create` method is used.

This style fits best when validating lots of fields one after another or inside array loops where changing `$rules` dynamically can make sense.

```
use \NilPortugues\Validator\ValidatorFactory;

$rules = ['positive', 'between:18:100:true'];
$ageValidator = ValidatorFactory::create('age', 'integer', $rules);

$result = $ageValidator->validate(28);
$errors = $ageValidator->getErrors();
```

#### 2.1.3. Extending from the BaseValidator

[](#213-extending-from-the-basevalidator)

Validators extending from `\NilPortugues\Validator\BaseValidator` are short and maintainable.

```
use \NilPortugues\Validator\BaseValidator;

class AgeValidator extends BaseValidator
{
    /**
     * @var string
     */
    protected $type = 'integer';

    /**
     * @var array
     */
    protected $rules = [
        'positive',
        'between:18:100:true'
    ];
}

$ageValidator = new AgeValidator();

$result = $ageValidator->validate('age', 28);
$errors = $ageValidator->getErrors();
```

Rules cannot be dynamically changed unless you provide the class a method to do so! For instance:

```
use \NilPortugues\Validator\BaseValidator;

class AgeValidator extends BaseValidator
{
    /**
     * @var string
     */
    protected $type = 'integer';

    /**
     * @var array
     */
    protected $rules = ['positive'];

    /**
     * @param array $rules
     */
    public function __construct(array $rules)
    {
        $this->rules = array_merge($this->rules, $rules);
    }
}

$ageValidator = new AgeValidator(['between:18:100:true']);

$result = $ageValidator->validate('age', 28);
$errors = $ageValidator->getErrors();
```

### 2.2. Stop on first error [↑](#index_block)

[](#22-stop-on-first-error-)

Sometimes, fast validation checks are needed when validating input data. This is possible by passing `true` as the second argument to the `validate` method.

For instance, in the following code `isBetween`is never executed.

```
use \NilPortugues\Validator\Validator;

$age = Validator::create()->isInteger('age');
$result = $age->isPositive()->isBetween(0, 100, true)->validate(-10, true);
```

3. Validation Message Translation [↑](#index_block)
===================================================

[](#3-validation-message-translation-)

The Input Validator features a default translation that can be found at `src/Errors/en_GB.php`. If no file path is provided when creating the validator instance, this file is used.

---

```
Feel free to submit a pull request or open an issue with more translations!

```

---

#### Customization

[](#customization)

Language can be changed anytime by providing a file following the same structure.

```
$translationFile = 'full/path/to/alternate-translation.php';

$validator = Validator::create($translationFile);

$stringValidator = $validator->isString('username');
$errors = $stringValidator->getErrors(); //error array in the provided language.
```

#### Available translations

[](#available-translations)

- en\_GB **English (British)**

4. Methods [↑](#index_block)
============================

[](#4-methods-)

All data type validators share 2 basic methods:

- **isRequired**
- **isNotNull**

Its usage is fairly simple:

```
$validator = Validator::create();

$username = $validator->isString('username');

$username->isRequired()->validate('Nil'); //true
$username->isNotNull()->validate(''); //false

//Or combined...
$username->isRequired()->isNotNull()->validate('Nil'); //true
```

For optional data, wrap the validator function within an if with an `isset($value)` clause and validate using `isNotNull`.

For instance, suppose gender is a value from 0 to 2 (male, female, other) and is a non-mandatory value:

```
$validator = Validator::create();

$genderValue = $validator->isInteger('gender');
$result = true;

if (isset($gender)) {
    $result = $genderValue->isNotNull()->isBetween(0, 2, true)->validate($gender);
}

return $result;
```

4.1 isString | string [↑](#index_block)
---------------------------------------

[](#41-isstring--string-)

String validation starts when creating a string field in the validator using the `isString` method.

```
$username = Validator::create()->isString('username');
```

The following chainable validation options are available for string data:

#### 4.1.1. isAlphanumeric  [↑](#index_block)

[](#411-isalphanumeric--)

##### 4.1.1.1 Instantiation of Validator class

[](#4111-instantiation-of-validator-class)

```
use \NilPortugues\Validator\Validator;

$string = Validator::create()->isString('propertyName');

$string->isAlphanumeric()->validate('28a'); // true
$string->isAlphanumeric()->validate('hello@example.com'); // false
```

##### 4.1.1.2. Factory using Validator::create

[](#4112-factory-using-validatorcreate)

```
use \NilPortugues\Validator\Validator;

$validator = Validator::create('propertyName', 'string', ['alphanumeric']);

$validator->validate('28a'); // true
$validator->validate('hello@example.com'); // false
```

##### 4.1.1.3. Extending from the BaseValidator

[](#4113-extending-from-the-basevalidator)

```
use \NilPortugues\Validator\BaseValidator;

class FieldValidator extends BaseValidator
{
    protected $type = 'string';

    protected $rules = ['alphanumeric'];
}

$fieldValidator = new FieldValidator();

$fieldValidator->validate('propertyName', '28a'); //true
$fieldValidator->validate('propertyName', 'hello@example.com');
```

#### 4.1.2. isAlpha  [↑](#index_block)

[](#412-isalpha--)

##### Example

[](#example)

```
$string = Validator::create()->isString('propertyName');

$string->isAlpha()->validate('Hello World'); // true
$string->isAlpha()->validate('28a'); // false
$string->isAlpha()->validate('hello@example.com'); // false
```

#### 4.1.3. isBetween  [↑](#index_block)

[](#413-isbetween--)

##### Example

[](#example-1)

```
$string = Validator::create()->isString('propertyName');

$string->isBetween(2, 4)->validate('Nilo'); //false
$string->isBetween(2, 4, true)->validate('Nilo'); //true
```

#### 4.1.4. isCharset  [↑](#index_block)

[](#414-ischarset--)

##### Example

[](#example-2)

```
$string = Validator::create()->isString('propertyName');

$string->isCharset(['UTF-8'])->validate('Portugués'); //true
```

#### 4.1.5. isAllConsonants  [↑](#index_block)

[](#415-isallconsonants--)

##### Example

[](#example-3)

```
$string = Validator::create()->isString('propertyName');

$string->isAllConsonants()->validate('a'); //false
$string->isAllConsonants()->validate('bs'); //true
```

#### 4.1.6. contains  [↑](#index_block)

[](#416-contains--)

##### Example

[](#example-4)

```
$string = Validator::create()->isString('propertyName');

$string->contains(123)->validate('AAAAAAA123A'); //true
$string->contains(123, true)->validate('AAAAAAA123A'); //false
```

#### 4.1.7. isControlCharacters  [↑](#index_block)

[](#417-iscontrolcharacters--)

##### Example

[](#example-5)

```
$string = Validator::create()->isString('propertyName');

$string->isControlCharacters()->validate("\n\t"); //true
$string->isControlCharacters()->validate("\nHello\tWorld"); //false
```

#### 4.1.8. isDigit  [↑](#index_block)

[](#418-isdigit--)

##### Example

[](#example-6)

```
$string = Validator::create()->isString('propertyName');

$string->isDigit()->validate('10'); //true

$string->isDigit()->validate('A'); //false
$string->isDigit()->validate(145.6); //false
```

#### 4.1.9. endsWith  [↑](#index_block)

[](#419-endswith--)

##### Example

[](#example-7)

```
$string = Validator::create()->isString('propertyName');

$string->endsWith('aaaA')->validate('AAAAAAAaaaA'); //true
$string->endsWith(123, true)->validate('AAAAAAA123'); //false
```

#### 4.1.10. equals  [↑](#index_block)

[](#4110-equals--)

##### Example

[](#example-8)

```
$string = Validator::create()->isString('propertyName');

$string->equals('hello')->validate('hello'); //true

$string->equals(1)->validate('1'); //true
$string->equals(1, true)->validate('1'); //false
```

#### 4.1.11. in  [↑](#index_block)

[](#4111-in--)

##### Example

[](#example-9)

```
$string = Validator::create()->isString('propertyName');

$string->in('a12245 asdhsjasd 63-211', true)->validate('5 asd'); //true
$string->in(122, true)->validate('a12245 asdhsjasd 63-211'); //false
```

#### 4.1.12. hasGraphicalCharsOnly  [↑](#index_block)

[](#4112-hasgraphicalcharsonly--)

##### Example

[](#example-10)

```
$string = Validator::create()->isString('propertyName');

$string->hasGraphicalCharsOnly()->validate('arf12'); //true
$string->hasGraphicalCharsOnly()->validate("asdf\n\r\t"); //false
```

#### 4.1.13. hasLength  [↑](#index_block)

[](#4113-haslength--)

##### Example

[](#example-11)

```
$string = Validator::create()->isString('propertyName');

$string->hasLength(5)->validate('abcdefgh'); //false
$string->hasLength(8)->validate('abcdefgh'); //true
```

#### 4.1.14. isLowercase  [↑](#index_block)

[](#4114-islowercase--)

##### Example

[](#example-12)

```
$string = Validator::create()->isString('propertyName');

$string->isLowercase()->validate('strtolower'); //true
$string->isLowercase()->validate('strtolOwer'); //false
```

#### 4.1.15. notEmpty  [↑](#index_block)

[](#4115-notempty--)

##### Example

[](#example-13)

```
$string = Validator::create()->isString('propertyName');

$string->notEmpty()->validate('a'); //true
$string->notEmpty()->validate(''); //false
```

#### 4.1.16. noWhitespace  [↑](#index_block)

[](#4116-nowhitespace--)

##### Example

[](#example-14)

```
$string = Validator::create()->isString('propertyName');

$string->noWhitespace()->validate('aaaaa'); //true
$string->noWhitespace()->validate('lorem ipsum'); //false
```

#### 4.1.17. hasPrintableCharsOnly  [↑](#index_block)

[](#4117-hasprintablecharsonly--)

##### Example

[](#example-15)

```
$string = Validator::create()->isString('propertyName');

$string->hasPrintableCharsOnly()->validate("LMKA0$%_123"); //true
$string->hasPrintableCharsOnly()->validate("LMKA0$%\t_123"); //false
```

#### 4.1.18. isPunctuation  [↑](#index_block)

[](#4118-ispunctuation--)

##### Example

[](#example-16)

```
$string = Validator::create()->isString('propertyName');

$string->isPunctuation()->validate('&,.;'); //true
$string->isPunctuation()->validate('a'); //false
```

#### 4.1.19. matchesRegex  [↑](#index_block)

[](#4119-matchesregex--)

##### Example

[](#example-17)

```
$string = Validator::create()->isString('propertyName');

$string->matchesRegex('/[a-z]/')->validate('a'); //true
$string->matchesRegex('/[a-z]/')->validate('A'); //false
```

#### 4.1.20. isSlug  [↑](#index_block)

[](#4120-isslug--)

##### Example

[](#example-18)

```
$string = Validator::create()->isString('propertyName');

$string->isSlug()->validate('hello-world-yeah'); //true

$string->isSlug()->validate('-hello-world-yeah'); //false
$string->isSlug()->validate('hello-world-yeah-'); //false
$string->isSlug()->validate('hello-world----yeah'); //false
```

#### 4.1.21. isSpace  [↑](#index_block)

[](#4121-isspace--)

##### Example

[](#example-19)

```
$string = Validator::create()->isString('propertyName');

$string->isSpace()->validate('    '); //true
$string->isSpace()->validate('e e'); //false
```

#### 4.1.22. startsWith  [↑](#index_block)

[](#4122-startswith--)

##### Example

[](#example-20)

```
$string = Validator::create()->isString('propertyName');

$string->startsWith('aaaA')->validate('aaaAAAAAAAA'); //true
$string->startsWith(123, true)->validate('123AAAAAAA'); //false
```

#### 4.1.23. isUppercase  [↑](#index_block)

[](#4123-isuppercase--)

##### Example

[](#example-21)

```
$string = Validator::create()->isString('propertyName');

$string->isUppercase()->validate('AAAAAA'); //true
$string->isUppercase()->validate('aaaa'); //false
```

#### 4.1.24. isVersion  [↑](#index_block)

[](#4124-isversion--)

##### Example

[](#example-22)

```
$string = Validator::create()->isString('propertyName');

$string->isVersion()->validate('1.0.2'); //true
$string->isVersion()->validate('1.0.2-beta'); //true
$string->isVersion()->validate('1.0'); //true
$string->isVersion()->validate('1.0.2 beta'); //false
```

#### 4.1.25. isVowel  [↑](#index_block)

[](#4125-isvowel--)

##### Example

[](#example-23)

```
$string = Validator::create()->isString('propertyName');

$string->isVowel()->validate('aeA'); //true
$string->isVowel()->validate('cds'); //false
```

#### 4.1.26. isHexDigit  [↑](#index_block)

[](#4126-ishexdigit--)

##### Example

[](#example-24)

```
$string = Validator::create()->isString('propertyName');

$string->isHexDigit()->validate(100); //true
$string->isHexDigit()->validate('h0000'); //false
```

#### 4.1.27. hasLowercase  [↑](#index_block)

[](#4127-haslowercase--)

##### Example

[](#example-25)

```
$string = Validator::create()->isString('propertyName');

$string->hasLowercase()->validate('HELLOWOrLD'); //true
$string->hasLowercase(3)->validate('HeLLoWOrLD'); //true

$string->hasLowercase()->validate('HELLOWORLD'); //false
$string->hasLowercase(3)->validate('el'); //false
```

#### 4.1.28. hasUppercase  [↑](#index_block)

[](#4128-hasuppercase--)

##### Example

[](#example-26)

```
$string = Validator::create()->isString('propertyName');

$string->hasUppercase()->validate('hello World'); //true
$string->hasUppercase(2)->validate('Hello World'); //true

$string->hasUppercase()->validate('hello world'); //false
$string->hasUppercase(2)->validate('helloWorld'); //false
```

#### 4.1.29. hasNumeric  [↑](#index_block)

[](#4129-hasnumeric--)

##### Example

[](#example-27)

```
$string = Validator::create()->isString('propertyName');

$string->hasNumeric()->validate('hell0 W0rld'); //true
$string->hasNumeric(3)->validate('H3ll0 W0rld'); //true

$string->hasNumeric()->validate('hello world'); //false
$string->hasNumeric(2)->validate('h3lloWorld'); //false
```

#### 4.1.30. hasSpecialCharacters  [↑](#index_block)

[](#4130-hasspecialcharacters--)

##### Example

[](#example-28)

```
$string = Validator::create()->isString('propertyName');

$string->hasSpecialCharacters()->validate('hell0@W0rld'); //true
$string->hasSpecialCharacters(2)->validate('H3ll0@W0@rld'); //true

$string->hasSpecialCharacters()->validate('hello world'); //false
$string->hasSpecialCharacters(2)->validate('h3llo@World'); //false
```

#### 4.1.31. isEmail  [↑](#index_block)

[](#4131-isemail--)

##### Example

[](#example-29)

```
$string = Validator::create()->isString('propertyName');

$string->isEmail()->validate('hello@world.com'); //true
$string->isEmail()->validate('hello.earth@world.com'); //true
$string->isEmail()->validate('hello.earth+moon@world.com'); //true
$string->isEmail()->validate('hello@subdomain.world.com'); //true
$string->isEmail()->validate('hello.earth@subdomain.world.com'); //true
$string->isEmail()->validate('hello.earth+moon@subdomain.world.com'); //true
$string->isEmail()->validate('hello.earth+moon@127.0.0.1'); //true

$string->isEmail()->validate('hello.earth+moon@localhost'); //false
```

#### 4.1.32. isUrl  [↑](#index_block)

[](#4132-isurl--)

##### Example

[](#example-30)

```
$string = Validator::create()->isString('propertyName');

$string->->isUrl()->validate('http://google.com'); //true
$string->->isUrl()->validate('http://google.com/robots.txt'); //true
$string->->isUrl()->validate('https://google.com'); //true
$string->->isUrl()->validate('https://google.com/robots.txt'); //true
$string->->isUrl()->validate('//google.com'); //true
$string->->isUrl()->validate('//google.com/robots.txt'); //true
```

#### 4.1.33. isUUID($strict = true)  [↑](#index_block)

[](#4133-isuuidstrict--true--)

##### Example

[](#example-31)

```
$string = Validator::create()->isString('propertyName');

$string->isUUID()->validate('6ba7b810-9dad-11d1-80b4-00c04fd430c8'); //true
$string->isUUID()->validate('6ba7b811-9dad-11d1-80b4-00c04fd430c8'); //true
$string->isUUID()->validate('6ba7b812-9dad-11d1-80b4-00c04fd430c8'); //true
$string->isUUID()->validate('6ba7b814-9dad-11d1-80b4-00c04fd430c8'); //true
$string->isUUID()->validate('00000000-0000-0000-0000-000000000000'); //true

$string->isUUID(false)->validate('6ba7b810-9dad-11d1-80b4-00c04fd430c8'); //true
$string->isUUID(false)->validate('6ba7b811-9dad-11d1-80b4-00c04fd430c8'); //true
$string->isUUID(false)->validate('6ba7b812-9dad-11d1-80b4-00c04fd430c8'); //true
$string->isUUID(false)->validate('6ba7b814-9dad-11d1-80b4-00c04fd430c8'); //true
$string->isUUID(false)->validate('00000000-0000-0000-0000-000000000000'); //true

$string->isUUID()->validate('{6ba7b810-9dad-11d1-80b4-00c04fd430c8}'); //false
$string->isUUID()->validate('216f-ff40-98d9-11e3-a5e2-0800-200c-9a66'); //false
$string->isUUID()->validate('{216fff40-98d9-11e3-a5e2-0800200c9a66}'); //false
$string->isUUID()->validate('216fff4098d911e3a5e20800200c9a66'); //false

$string->isUUID(false)->validate('{6ba7b810-9dad-11d1-80b4-00c04fd430c8}'); //true
$string->isUUID(false)->validate('216f-ff40-98d9-11e3-a5e2-0800-200c-9a66'); //true
$string->isUUID(false)->validate('{216fff40-98d9-11e3-a5e2-0800200c9a66}'); //true
$string->isUUID(false)->validate('216fff4098d911e3a5e20800200c9a66'); //true
```

4.2 isInteger | integer and isFloat | float [↑](#index_block)
-------------------------------------------------------------

[](#42-isinteger--integer-and-isfloat--float-)

Number validation comes in two flavours, `Integers` and `Floats`. Both validators share the same method interface, but internal implementation is different.

To use these validators, do as follows:

```
$integer = Validator::create()->isInteger('propertyName');

$float = $validator->isFloat('propertyName');
```

#### 4.2.1. isNotZero  [↑](#index_block)

[](#421-isnotzero---)

##### Example

[](#example-32)

```
$integer = Validator::create()->isInteger('propertyName');

$integer->isNotZero()->validate(1); //true
$integer->isNotZero()->validate(0); //false
```

#### 4.2.2. isPositive  [↑](#index_block)

[](#422-ispositive---)

##### Example

[](#example-33)

```
$integer = Validator::create()->isInteger('propertyName');

$integer->isPositive()->validate(1); //true
$integer->isPositive()->validate(-10); //false
$integer->isPositive()->validate(0); //false
```

#### 4.2.3. isPositiveOrZero  [↑](#index_block)

[](#423-ispositiveorzero--)

##### Example

[](#example-34)

```
$integer = Validator::create()->isInteger('propertyName');

$integer->isPositiveOrZero()->validate(1); //true
$integer->isPositiveOrZero()->validate(-10); //false
$integer->isPositiveOrZero()->validate(0); //true
```

#### 4.2.4. isNegative  [↑](#index_block)

[](#424-isnegative---)

##### Example

[](#example-35)

```
$integer = Validator::create()->isInteger('propertyName');

$integer->isNegative()->validate(-10); //true
$integer->isNegative()->validate(1); //false
$integer->isNegative()->validate(0); //false
```

#### 4.2.5. isNegativeOrZero  [↑](#index_block)

[](#425-isnegativeorzero--)

##### Example

[](#example-36)

```
$integer = Validator::create()->isInteger('propertyName');

$integer->isNegativeOrZero()->validate(-10); //true
$integer->isNegativeOrZero()->validate(1); //false
$integer->isNegativeOrZero()->validate(0); //true
```

#### 4.2.6. isBetween  [↑](#index_block)

[](#426-isbetween---)

##### Example

[](#example-37)

```
$integer = Validator::create()->isInteger('propertyName');

$integer->isBetween(10,20, false)->validate(13); //true
$integer->isBetween(10, 20, true)->validate(10); //false
```

#### 4.2.7. isOdd  [↑](#index_block)

[](#427-isodd---)

##### Example

[](#example-38)

```
$integer = Validator::create()->isInteger('propertyName');

$integer->isOdd()->validate(3); //true
$integer->isOdd()->validate(2); //false
```

#### 4.2.8. isEven  [↑](#index_block)

[](#428-iseven---)

##### Example

[](#example-39)

```
$integer = Validator::create()->isInteger('propertyName');

$integer->isEven()->validate(2); //true
$integer->isEven()->validate(3); //false
```

#### 4.2.9. isMultiple  [↑](#index_block)

[](#429-ismultiple---)

##### Example

[](#example-40)

```
$integer = Validator::create()->isInteger('propertyName');

$integer->isMultiple(2)->validate(4); //true
$integer->isMultiple(2)->validate(3); //false
```

4.3 isObject | object [↑](#index_block)
---------------------------------------

[](#43-isobject--object-)

#### 4.3.1. isInstanceOf [↑](#index_block)

[](#431-isinstanceof-)

##### Example

[](#example-41)

```
$object = Validator::create()->isObject('propertyName');

$object->isInstanceOf('DateTime')->validate(new \DateTime()); //true

$object->isInstanceOf('DateTime')->validate(new \stdClass()); //false
$object->isInstanceOf('DateTime')->validate('a'); //false
```

#### 4.3.2. hasProperty [↑](#index_block)

[](#432-hasproperty-)

##### Example

[](#example-42)

```
$object = Validator::create()->isObject('propertyName');

$dummy = new Dummy();

$object->hasProperty('userName')->validate($dummy); //true
$object->hasProperty('password')->validate($dummy); //false
```

#### 4.3.3. hasMethod [↑](#index_block)

[](#433-hasmethod-)

##### Example

[](#example-43)

```
$object = Validator::create()->isObject('propertyName');

$dummy = new Dummy();

$object->hasMethod('getUserName')->validate($dummy); //true
$object->hasMethod('getPassword')->validate($dummy); //false
```

#### 4.3.4. hasParentClass [↑](#index_block)

[](#434-hasparentclass-)

##### Example

[](#example-44)

```
$object = Validator::create()->isObject('propertyName');

$object->hasParentClass()->validate(new Dummy()); //true
$object->hasParentClass()->validate(new \stdClass()); //false
```

#### 4.3.5. isChildOf [↑](#index_block)

[](#435-ischildof-)

##### Example

[](#example-45)

```
$object = Validator::create()->isObject('propertyName');

$dummy = new Dummy(); // class Dummy extends \DateTime

$object->isChildOf('DateTime')->validate($dummy); //true
$object->isChildOf('DateTimeZone')->validate($dummy); //false
```

#### 4.3.6. inheritsFrom [↑](#index_block)

[](#436-inheritsfrom-)

##### Example

[](#example-46)

```
$object = Validator::create()->isObject('propertyName');

$dummy = new Dummy(); // class Dummy extends \DateTime

$object->inheritsFrom('DateTime')->validate($dummy); //true
$object->inheritsFrom('DateTimeZone')->validate($dummy); //false
```

#### 4.3.7. hasInterface [↑](#index_block)

[](#437-hasinterface-)

##### Example

[](#example-47)

```
$object = Validator::create()->isObject('propertyName');

$object->hasInterface('Tests\NilPortugues\Validator\Resources\DummyInterface')->validate($dummy); //true
$object->inheritsFrom('DateTimeZone')->validate($dummy); //false
```

4.4 isDateTime | date\_time [↑](#index_block)
---------------------------------------------

[](#44-isdatetime--date_time--)

DateTime Validator accepts `\DateTime` objects and `strings` variables representing **valid date formats**.

As you will see in the following examples, either two are allowed as parameters for any `date` value.

#### 4.4.1. isAfter  [↑](#index_block)

[](#441-isafter--)

##### Example

[](#example-48)

```
$datetime = Validator::create()->isDateTime('propertyName');
$date1 = '2014-01-01 00:00:00';
$date2 = new \DateTime($date1);

$limit1 = '2013-12-31 23:59:59';

$datetime->isAfter($limit1, false)->validate($date1); // true
$datetime->isAfter($limit1, false)->validate($date2); // true

$datetime->isAfter($date1, true)->validate($date1); // true
$datetime->isAfter($date2, true)->validate($date2); // true

$limit2 = '2015-01-01 00:00:00';

$datetime->isAfter($limit2)->validate($date1); // false
$datetime->isAfter($limit2)->validate($date2); // false
```

#### 4.4.2. isBefore  [↑](#index_block)

[](#442-isbefore--)

##### Example

[](#example-49)

```
$datetime = Validator::create()->isDateTime('propertyName');
$date1 = '2012-01-01 00:00:00';
$date2 = new \DateTime($date1);

$limit1 = '2013-12-31 23:59:59';

$datetime->isBefore($limit1, false)->validate($date1); // true
$datetime->isBefore($limit1, false)->validate($date2); // true

$datetime->isBefore($date1, true)->validate($date1); // true
$datetime->isBefore($date2, true)->validate($date2); // true

$limit2 = '2010-01-01 00:00:00';

$datetime->isBefore($limit2)->validate($date1); // false
$datetime->isBefore($limit2)->validate($date2); // false
```

#### 4.4.3. isBetween  [↑](#index_block)

[](#443-isbetween--)

##### Example

[](#example-50)

```
$datetime = Validator::create()->isDateTime('propertyName');
$date1 = '2014-01-01 00:00:00';
$date2 = new \DateTime($date1);

$minDate = '2013-01-01 00:00:00';
$maxDate = '2015-01-01 00:00:00';

$datetime->isBetween($minDate, $maxDate, false)->validate($date1); // true
$datetime->isBetween($minDate, $maxDate, false)->validate($date2); // true

$datetime->isBetween($minDate, $maxDate, true)->validate($date1); // true
$datetime->isBetween($minDate, $maxDate, true)->validate($date2); // true

$minDate = '2013-12-01 00:00:00';
$maxDate = '2013-12-30 00:00:00';

$datetime->isBetween($minDate, $maxDate, false)->validate($date1); // false
$datetime->isBetween($minDate, $maxDate, true)->validate($date1); // false
```

#### 4.4.4. isWeekend  [↑](#index_block)

[](#444-isweekend--)

##### Example

[](#example-51)

```
$datetime = Validator::create()->isDateTime('propertyName');

$datetime->isWeekend()->validate('2014-09-20'); // true
$datetime->isWeekend()->validate('2014-09-22'); // false
```

#### 4.4.5. isWeekday  [↑](#index_block)

[](#445-isweekday--)

##### Example

[](#example-52)

```
$datetime = Validator::create()->isDateTime('propertyName');

$datetime->isWeekday()->validate('2014-09-20'); // false
$datetime->isWeekday()->validate('2014-09-22'); // true
```

#### 4.4.6. isMonday  [↑](#index_block)

[](#446-ismonday--)

##### Example

[](#example-53)

```
$datetime = Validator::create()->isDateTime('propertyName');

$datetime->isMonday()->validate('2014-09-22'); // true
```

#### 4.4.7. isTuesday  [↑](#index_block)

[](#447-istuesday--)

##### Example

[](#example-54)

```
$datetime = Validator::create()->isDateTime('propertyName');

$datetime->isTuesday()->validate('2014-09-23'); // true
```

#### 4.4.8. isWednesday  [↑](#index_block)

[](#448-iswednesday--)

##### Example

[](#example-55)

```
$validator = Validator::create();
$datetime = $validator->isDateTime('propertyName');

$datetime->isWednesday()->validate('2014-09-24'); // true
```

#### 4.4.9. isThursday  [↑](#index_block)

[](#449-isthursday--)

##### Example

[](#example-56)

```
$datetime = Validator::create()->isDateTime('propertyName');

$datetime->isThursday()->validate('2014-09-25'); // true
```

#### 4.4.10. isFriday  [↑](#index_block)

[](#4410-isfriday--)

##### Example

[](#example-57)

```
$datetime = Validator::create()->isDateTime('propertyName');

$datetime->isFriday()->validate('2014-09-26'); // true
```

#### 4.4.11. isSaturday  [↑](#index_block)

[](#4411-issaturday--)

##### Example

[](#example-58)

```
$datetime = Validator::create()->isDateTime('propertyName');

$datetime->isSaturday()->validate('2014-09-27'); // true
```

#### 4.4.12. isSunday  [↑](#index_block)

[](#4412-issunday--)

##### Example

[](#example-59)

```
$datetime = Validator::create()->isDateTime('propertyName');

$datetime->isSunday()->validate('2014-09-28'); // true
```

#### 4.4.13. isToday  [↑](#index_block)

[](#4413-istoday--)

##### Example

[](#example-60)

```
$datetime = Validator::create()->isDateTime('propertyName');

$date = new \DateTime('now');

$datetime->isToday()->validate($date); // true
```

#### 4.4.14. isYesterday  [↑](#index_block)

[](#4414-isyesterday--)

##### Example

[](#example-61)

```
$datetime = Validator::create()->isDateTime('propertyName');

$date = new \DateTime('now -1 day');

$datetime->isYesterday()->validate($date); // true
```

#### 4.4.15. isTomorrow  [↑](#index_block)

[](#4415-istomorrow--)

##### Example

[](#example-62)

```
$datetime = Validator::create()->isDateTime('propertyName');

$date = new \DateTime('now +1 day');

$datetime->isTomorrow()->validate($date); // true
```

#### 4.4.16. isLeapYear  [↑](#index_block)

[](#4416-isleapyear--)

##### Example

[](#example-63)

```
$datetime = Validator::create()->isDateTime('propertyName');

$date = new \DateTime('2016-01-01');

$datetime->isLeapYear()->validate($date); // true
```

#### 4.4.17. isMorning  [↑](#index_block)

[](#4417-ismorning--)

##### Example

[](#example-64)

```
$datetime = Validator::create()->isDateTime('propertyName');
```

#### 4.4.18. isAftenoon  [↑](#index_block)

[](#4418-isaftenoon--)

##### Example

[](#example-65)

```
$datetime = Validator::create()->isDateTime('propertyName');
```

#### 4.4.19. isEvening  [↑](#index_block)

[](#4419-isevening--)

##### Example

[](#example-66)

```
$datetime = Validator::create()->isDateTime('propertyName');
```

#### 4.4.20. isNight  [↑](#index_block)

[](#4420-isnight--)

##### Example

[](#example-67)

```
$datetime = Validator::create()->isDateTime('propertyName');
```

4.5 isArray | array [↑](#index_block)
-------------------------------------

[](#45-isarray--array-)

Collections are data structures that hold other data structures or same type variables.

Supported PHP data structures for the Collection validator are:

- array
- ArrayObject
- SplFixedArray

#### 4.5.1. each  [↑](#index_block)

[](#451-each--)

##### Example

[](#example-68)

```
$validator = Validator::create();
$collection = $validator->isArray('propertyName');

$valueIsString = $validator->isString('value')->isAlpha();
$keyIsInteger = $validator->isInteger('key')->isPositive();

$array = ['hello','world'];
$arrayObject = new \ArrayObject($array);
$fixedArray = (new \SplFixedArray())->fromArray($array);

$collection->each($valueIsString)->validate($array); //true
$collection->each($valueIsString, $keyIsInteger)->validate($array); //true

$collection->each($valueIsString)->validate($arrayObject); //true
$collection->each($valueIsString, $keyIsInteger)->validate($arrayObject); //true

$collection->each($valueIsString)->validate($fixedArray); //true
$collection->each($valueIsString, $keyIsInteger)->validate($fixedArray); //true
```

#### 4.5.2. hasKeyFormat  [↑](#index_block)

[](#452-haskeyformat--)

##### Example

[](#example-69)

```
$validator = Validator::create();
$collection = $validator->isArray('propertyName');

$array = ['one' => 'hello', 'two' => 'world'];
$arrayObject = new \ArrayObject($array);
$fixedArray = (new \SplFixedArray())->fromArray(array_values($array));

$keyIsString = $validator->isString('key')->isAlpha();
$keyIsInteger = $validator->isInteger('key')->isPositive();

$collection->hasKeyFormat($keyIsString)->validate($array); //true
$collection->hasKeyFormat($keyIsString)->validate($arrayObject); //true
$collection->hasKeyFormat($keyIsInteger)->validate($fixedArray); //true
```

#### 4.5.3. endsWith  [↑](#index_block)

[](#453-endswith--)

##### Example

[](#example-70)

```
$collection = Validator::create()->isArray('propertyName');

$array = ['one' => 'hello', 'two' => 1];
$arrayObject = new \ArrayObject($array);
$fixedArray = (new \SplFixedArray())->fromArray(array_values($array));

$collection->endsWith('1')->validate($array); //true
$collection->endsWith('1')->validate($arrayObject); //true
$collection->endsWith('1')->validate($fixedArray); //true

$collection->endsWith('1', true)->validate($array); //false
$collection->endsWith('1', true)->validate($arrayObject); //false
$collection->endsWith('1', true)->validate($fixedArray); //false
```

#### 4.5.4. contains  [↑](#index_block)

[](#454-contains--)

##### Example

[](#example-71)

```
$collection = Validator::create()->isArray('propertyName');

$array = ['one' => 'hello', 'two' => 1];
$arrayObject = new \ArrayObject($array);
$fixedArray = (new \SplFixedArray())->fromArray(array_values($array));

$collection->contains('hello')->validate($array); //true
$collection->contains('hello')->validate($arrayObject); //true
$collection->contains('hello')->validate($fixedArray); //true

$collection->contains(1, true)->validate($array); //true
$collection->contains(1, true)->validate($arrayObject); //true
$collection->contains(1, true)->validate($fixedArray); //true
```

#### 4.5.5. hasKey  [↑](#index_block)

[](#455-haskey--)

##### Example

[](#example-72)

```
$collection = Validator::create()->isArray('propertyName');

$array = ['one' => 'hello', 'two' => 1];
$arrayObject = new \ArrayObject($array);
$fixedArray = (new \SplFixedArray())->fromArray(array_values($array));

$collection->hasKey('one')->validate($array)); //true
$collection->hasKey('one')->validate($arrayObject)); //true
$collection->hasKey(0)->validate($fixedArray)); //true

$array = [];
$arrayObject = new \ArrayObject($array);
$fixedArray = (new \SplFixedArray())->fromArray(array_values($array));

$collection->hasKey(0)->validate($array)); //false
$collection->hasKey(0)->validate($arrayObject)); //false
$collection->hasKey(0)->validate($fixedArray)); //false
```

#### 4.5.6. hasLength  [↑](#index_block)

[](#456-haslength--)

##### Example

[](#example-73)

```
$collection = Validator::create()->isArray('propertyName');

$array = ['one' => 'hello', 'two' => 1];
$arrayObject = new \ArrayObject($array);
$fixedArray = (new \SplFixedArray())->fromArray(array_values($array));

$collection->hasLength(2)->validate($array)); //true
$collection->hasLength(2)->validate($arrayObject)); //true
$collection->hasLength(2)->validate($fixedArray)); //true

$array = [];
$arrayObject = new \ArrayObject($array);
$fixedArray = (new \SplFixedArray())->fromArray(array_values($array));

$collection->hasLength(0)->validate($array)); //true
$collection->hasLength(0)->validate($arrayObject)); //true
$collection->hasLength(0)->validate($fixedArray)); //true
```

#### 4.5.7. isNotEmpty  [↑](#index_block)

[](#457-isnotempty--)

##### Example

[](#example-74)

```
$collection = Validator::create()->isArray('propertyName');

$array = ['one' => 'hello', 'two' => 1];
$arrayObject = new \ArrayObject($array);
$fixedArray = (new \SplFixedArray())->fromArray(array_values($array));

$collection->isNotEmpty()->validate($array)); //true
$collection->isNotEmpty()->validate($arrayObject)); //true
$collection->isNotEmpty()->validate($fixedArray)); //true

$array = [];
$arrayObject = new \ArrayObject($array);
$fixedArray = (new \SplFixedArray())->fromArray(array_values($array));

$collection->isNotEmpty()->validate($array)); //false
$collection->isNotEmpty()->validate($arrayObject)); //false
$collection->isNotEmpty()->validate($fixedArray)); //false
```

#### 4.5.8. startsWith  [↑](#index_block)

[](#458-startswith--)

##### Example

[](#example-75)

```
$collection = Validator::create()->isArray('propertyName');

$array = [1, 2, 3];
$arrayObject = new \ArrayObject($array);
$fixedArray = (new \SplFixedArray())->fromArray($array);

$collection->startsWith(1)->validate($array)); //true
$collection->startsWith(1)->validate($arrayObject)); //true
$collection->startsWith(1)->validate($fixedArray)); //true

$collection->startsWith('1', true)->validate($array)); //false
$collection->startsWith('1', true)->validate($arrayObject)); //false
$collection->startsWith('1', true)->validate($fixedArray)); //false
```

4.6 isFileUpload | file\_upload [↑](#index_block)
-------------------------------------------------

[](#46-isfileupload--file_upload-)

FileUpload validation is one of the most boring parts of web development, but this validator makes things a breeze.

### One file validation

[](#one-file-validation)

Using FileUpload validator alone, you can validate single file uploads.

```

```

On the server side, validation is done as follows:

```
$fileValidator = Validator::create()->isFileUpload('image');

$fileValidator
     ->isBetween(0, 3, 'MB', true)
     ->isMimeType(['image/png', 'image/gif', 'image/jpeg'])
     ->hasValidUploadDirectory('./uploads/images')
     ->notOverwritingExistingFile('./uploads/images')
     ->validate('image');
```

### Multiple file validation

[](#multiple-file-validation)

For instance, let's say file upload is done using the following form:

```

```

On the server side it is done exactly the same as before! Easy, right? :)

#### 4.6.1. isBetween($minSize, $maxSize, $inclusive = false)  [↑](#index_block)

[](#461-isbetweenminsize-maxsize-inclusive--false---)

##### Example

[](#example-76)

```
$file = Validator::create()->isFileUpload('image');

$file->isBetween(1, 3, 'MB', true)->validate('image');
```

#### 4.6.2. isMimeType(array $allowedTypes)  [↑](#index_block)

[](#462-ismimetypearray-allowedtypes---)

##### Example

[](#example-77)

```
$file = Validator::create()->isFileUpload('image');

$file->isMimeType(['image/png', 'image/gif', 'image/jpeg'])->validate('image');
```

#### 4.6.3. hasFileNameFormat(AbstractValidator $validator)  [↑](#index_block)

[](#463-hasfilenameformatabstractvalidator-validator---)

##### Example

[](#example-78)

```
$validator = Validator::create();
$file = $validator->isFileUpload('image');
$stringValidator = $validator->isString('image')->isAlpha();

$file->hasFileNameFormat($stringValidator)->validate('image');
```

#### 4.6.4. hasValidUploadDirectory($uploadDir)  [↑](#index_block)

[](#464-hasvaliduploaddirectoryuploaddir---)

##### Example

[](#example-79)

```
$file = Validator::create()->isFileUpload('image');

$file->hasValidUploadDirectory('./uploads/images')->validate('image');
```

#### 4.6.5. notOverwritingExistingFile($uploadDir)  [↑](#index_block)

[](#465-notoverwritingexistingfileuploaddir---)

##### Example

[](#example-80)

```
$file = Validator::create()->isFileUpload('image');

$file->notOverwritingExistingFile('./uploads/images')->validate('image');
```

#### 4.6.6. hasLength($size)  [↑](#index_block)

[](#466-haslengthsize---)

##### Example

[](#example-81)

```
$file = Validator::create()->isFileUpload('image');

$file->hasLength(1)->validate('image');
```

#### 4.6.7. isImage()  [↑](#index_block)

[](#467-isimage---)

##### Example

[](#example-82)

```
$file = Validator::create()->isFileUpload('image');

$file->isImage()->validate('image');
```

4. Quality Code [↑](#index_block)
=================================

[](#4-quality-code-)

Testing has been done using PHPUnit and [Travis-CI](https://travis-ci.org). All code has been tested to be compatible from PHP 5.4 up to PHP 5.6 and [HHVM](http://hhvm.com/).

To run the test suite, you need [Composer](http://getcomposer.org):

```
    php composer.phar install --dev
    php bin/phpunit
```

5. Author [↑](#index_block)
===========================

[](#5-author-)

Nil Portugués Calderó

-
-

6. License [↑](#index_block)
============================

[](#6-license-)

The Input Validator is licensed under the MIT license.

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 97.6% 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 ~57 days

Recently: every ~46 days

Total

8

Last Release

3887d ago

Major Versions

1.0.x-dev → v2.0.02015-04-30

v2.0.0 → v3.0.02015-09-18

### Community

Maintainers

![](https://www.gravatar.com/avatar/beba5d66ff48128da92c91850e69b9294768718bcdc12e61cc9ee8453d0d6b69?d=identicon)[nilportugues](/maintainers/nilportugues)

---

Top Contributors

[![nilportugues](https://avatars.githubusercontent.com/u/550948?v=4)](https://github.com/nilportugues "nilportugues (201 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (5 commits)")

---

Tags

error-handlingformform-validationphpphp7qualityvalidationvalidatorvalidatorvalidationdatainputnil portuguesnilportugues

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[egulias/email-validator

A library for validating emails against several RFCs

11.6k719.7M402](/packages/egulias-email-validator)[respect/validation

The most awesome validation engine ever created for PHP

6.0k39.0M407](/packages/respect-validation)[nilportugues/assert

A simple and elegant assertion and guard methods library for input validation.

1066.1k7](/packages/nilportugues-assert)[opis/json-schema

Json Schema Validator for PHP

64941.2M260](/packages/opis-json-schema)

PHPackages © 2026

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