Anekdotes Validator
, (*1)
A validator class to validate input data against validation types., (*2)
Installation
Install via composer in your project :, (*3)
composer require anekdotes/validator
Basic usage
Prepare an input array to validate and a rules array to validate against, (*4)
$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., (*5)
// 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
required
Check if the input is empty, (*6)
$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
Check if the input is empty, but only if an other input's value equals a specific value, (*7)
$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
Check if the input is empty if an other input is not empty, (*8)
$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
Check if the input is empty if an other input is empty, (*9)
$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
Check if the input is an integer, (*10)
$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
Check if the input is numeric, (*11)
$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
Check if the input is a date, (*12)
$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
Check if the input is different from follow-up values, (*13)
$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
Check if the input matches an email address, (*14)
$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
Check if the input matches a canadian postal code, (*15)
$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
Check if the input matches an american phone number, (*16)
$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
Check if the input is between a minimum and a maximum, (*17)
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, (*18)
$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
Check if the input is above a minimum, (*19)
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, (*20)
$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
Check if the input is under a maximum, (*21)
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, (*22)
$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
Check if the input has the exact size, (*23)
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, (*24)
$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
Check if the input has the exact string length provided (works the samne way as size, but only for strings), (*25)
$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
Check if the input matches a URL, (*26)
$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
Check if the input matches an existing URL, based on PHP's DNS Check, (*27)
$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
Check if the input had the same value as another input (useful for password checks), (*28)
$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
Check if the input contains only alphabetic characters, (*29)
$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
Check if the input contains only alphabetic and numeric characters, (*30)
$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
Check if the input contains only alphabetic,numeric and dashes characters, (*31)
$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
Check if the input is a date older than the provided :date, (*32)
$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
Check if the input if a date younger than the provided :date, (*33)
$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
Check if the input is an integer that contains exactly x digits, (*34)
$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
Check if the input, (*35)
Check if the input is an integer that contains between x and y digits, (*36)
$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
Checks that if a confirmation field has the same value that the other one (Perfect for password or email checks), (*37)
$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"];