Download the PHP package nilportugues/validator without Composer
On this page you can find all versions of the php package nilportugues/validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nilportugues/validator
More information about nilportugues/validator
Files in nilportugues/validator
Package validator
Short Description An efficient data validator for PHP
License MIT
Homepage http://nilportugues.com
Informations about the package validator
Input Validator
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
- 2. Usage
- 2.1. Validation made for everyone
- 2.1.1. Instantiation of Validator class
- 2.1.2. Factory using ValidatorFactory::create
- 2.1.3. Extending from the BaseValidator
- 2.2. Stop on first error
- 3. Validation Message Translation
- 4. Methods
- 4.1 isString | string
- [4.1.1. isAlphanumeric | alphanumeric] (#block4.1.1)
- 4.1.1.1. Instantiation of Validator class
- 4.1.1.2. Factory using Validator::create
- 4.1.1.3. Extending from the BaseValidator
- [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.1.1. isAlphanumeric | alphanumeric] (#block4.1.1)
- 4.2 isInteger | integer and isFloat | float
- [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
- [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
- [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
- [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
- [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.1 isString | string
- 4. Quality Code
- 5. Author
- 6. License
1. Installation ↑
The recommended way to install the Input Validator is through Composer. Run the following command to install it:
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 ↑
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
2.1.2. Factory using ValidatorFactory::create
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.
2.1.3. Extending from the BaseValidator
Validators extending from \NilPortugues\Validator\BaseValidator
are short and maintainable.
Rules cannot be dynamically changed unless you provide the class a method to do so! For instance:
2.2. 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.
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
Language can be changed anytime by providing a file following the same structure.
Available translations
- en_GB English (British)
4. Methods ↑
All data type validators share 2 basic methods:
- isRequired
- isNotNull
Its usage is fairly simple:
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:
4.1 isString | string ↑
String validation starts when creating a string field in the validator using the isString
method.
The following chainable validation options are available for string data:
4.1.1. isAlphanumeric ↑
4.1.1.1 Instantiation of Validator class
4.1.1.2. Factory using Validator::create
4.1.1.3. Extending from the BaseValidator
4.1.2. isAlpha ↑
Example
4.1.3. isBetween ↑
Example
4.1.4. isCharset ↑
Example
4.1.5. isAllConsonants ↑
Example
4.1.6. contains ↑
Example
4.1.7. isControlCharacters ↑
Example
4.1.8. isDigit ↑
Example
4.1.9. endsWith ↑
Example
4.1.10. equals ↑
Example
4.1.11. in ↑
Example
4.1.12. hasGraphicalCharsOnly ↑
Example
4.1.13. hasLength ↑
Example
4.1.14. isLowercase ↑
Example
4.1.15. notEmpty ↑
Example
4.1.16. noWhitespace ↑
Example
4.1.17. hasPrintableCharsOnly ↑
Example
4.1.18. isPunctuation ↑
Example
4.1.19. matchesRegex ↑
Example
4.1.20. isSlug ↑
Example
4.1.21. isSpace ↑
Example
4.1.22. startsWith ↑
Example
4.1.23. isUppercase ↑
Example
4.1.24. isVersion ↑
Example
4.1.25. isVowel ↑
Example
4.1.26. isHexDigit ↑
Example
4.1.27. hasLowercase ↑
Example
4.1.28. hasUppercase ↑
Example
4.1.29. hasNumeric ↑
Example
4.1.30. hasSpecialCharacters ↑
Example
4.1.31. isEmail ↑
Example
4.1.32. isUrl ↑
Example
4.1.33. isUUID($strict = true) ↑
Example
4.2 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:
4.2.1. isNotZero ↑
Example
4.2.2. isPositive ↑
Example
4.2.3. isPositiveOrZero ↑
Example
4.2.4. isNegative ↑
Example
4.2.5. isNegativeOrZero ↑
Example
4.2.6. isBetween ↑
Example
4.2.7. isOdd ↑
Example
4.2.8. isEven ↑
Example
4.2.9. isMultiple ↑
Example
4.3 isObject | object ↑
4.3.1. isInstanceOf ↑
Example
4.3.2. hasProperty ↑
Example
4.3.3. hasMethod ↑
Example
4.3.4. hasParentClass ↑
Example
4.3.5. isChildOf ↑
Example
4.3.6. inheritsFrom ↑
Example
4.3.7. hasInterface ↑
Example
4.4 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 ↑
Example
4.4.2. isBefore ↑
Example
4.4.3. isBetween ↑
Example
4.4.4. isWeekend ↑
Example
4.4.5. isWeekday ↑
Example
4.4.6. isMonday ↑
Example
4.4.7. isTuesday ↑
Example
4.4.8. isWednesday ↑
Example
4.4.9. isThursday ↑
Example
4.4.10. isFriday ↑
Example
4.4.11. isSaturday ↑
Example
4.4.12. isSunday ↑
Example
4.4.13. isToday ↑
Example
4.4.14. isYesterday ↑
Example
4.4.15. isTomorrow ↑
Example
4.4.16. isLeapYear ↑
Example
4.4.17. isMorning ↑
Example
4.4.18. isAftenoon ↑
Example
4.4.19. isEvening ↑
Example
4.4.20. isNight ↑
Example
4.5 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 ↑
Example
4.5.2. hasKeyFormat ↑
Example
4.5.3. endsWith ↑
Example
4.5.4. contains ↑
Example
4.5.5. hasKey ↑
Example
4.5.6. hasLength ↑
Example
4.5.7. isNotEmpty ↑
Example
4.5.8. startsWith ↑
Example
4.6 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
Using FileUpload validator alone, you can validate single file uploads.
On the server side, validation is done as follows:
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) ↑
Example
4.6.2. isMimeType(array $allowedTypes) ↑
Example
4.6.3. hasFileNameFormat(AbstractValidator $validator) ↑
Example
4.6.4. hasValidUploadDirectory($uploadDir) ↑
Example
4.6.5. notOverwritingExistingFile($uploadDir) ↑
Example
4.6.6. hasLength($size) ↑
Example
4.6.7. isImage() ↑
Example
4. Quality Code ↑
Testing has been done using PHPUnit and Travis-CI. All code has been tested to be compatible from PHP 5.4 up to PHP 5.6 and HHVM.
To run the test suite, you need Composer:
5. Author ↑
Nil Portugués Calderó
6. License ↑
The Input Validator is licensed under the MIT license.