This is a simple reusable validator class for PHP.
composer require cloudonaut/validator
Include it into your scripts by using composer autoload and create a new validator object.
require './vendor/autoload.php';
use Cloudonaut\Lib\Validator;
$validator = new Validator();
Just copy the Validator.php file from the src folder to your project and include it into your scripts. Than create a new validator object.
require_once './PATH/TO/Validator.php';
use Cloudonaut\Lib\Validator;
$validator = new Validator();
The validator stores violations automatically when there is a violation against a rule. To check is a validator has violations you can use the hasViolations() function. To get an array of the violations you can call getViolations(). To add violations you can use one of the many build in check functions or simply add a violation to the list using your own code to check the value.
Check if a value is not empty. If it is empty a violation is added to the validator object.
$value = "";
$msg = "Value can't be empty";
$validator->isNotEmpty($value, $msg);
if ($validator->hasViolations())
{
implode("," $validator->getViolations());
}
Output
Value can't be empty
In this example we need the value to match 7 as a simple example. But you can build on this example to do other stuff with value. For example: Call another api and based on the result validate the value, check the value against a database table etc...
$value = 8;
$msg = "Value must be 7";
if ($value != 7)
{
$validator->addViolation($msg);
}
if ($validator->hasViolations())
{
implode("," $validator->getViolations());
}
output
Value must be 7
You can check the same or other values as many as you want. For example. A e-mail adres must be filled in and a name. But a name is text only.
$name = "R2D2"; // wrong name
$mail="test.example.com"; //wrong mail
$validator->isNotEmpty($name, "Name can't be empty");
$validator->isNotEmpty($mail, "Mail can't be empty");
$validator->isTextOnly($name, "Name can't have numbers");
$validator->isEmail($name, "Mails is not in the correct format");
if ($validator->hasViolations())
{
implode(", " $validator->getViolations());
}
output
Name can't have numbers, Mails is not in the correct format
Adds a violation with the given message
Checks if the validator has violations. It will return true when there are violations or false when there are no violations.
Returns an array of the messages of all the violations. If there are no violations it just returns an empty array.
All functions will also return true when no violation is added and false when the value violates the rule.
Adds a violation when the value is empty.
Adds a violation when the value is not alphanumeric
Adds a violation when the value is not containing only letters.
Adds a violation when the value is not a number.
Adds a violation when the value contains anything else than number characters.
Adds a violation when the value is not between the min and max value.
Adds a violation when the value is not containing:
- At least one lower case letter
- At least one upper case letter
- At least one number or symbol
- and should be between 8 and 20 characters long
Adds a violation when the value is not a date in the folowing format: yyyy-mm-dd
Adds a violation when the value is not a time indication in the folowing format: hh:mm:ss
Adds a violation when the value is not an e-mail address
Adds a violation when the value is not an URL.
Adds a violation when the value is not an IP address. (v4)
Adds a violation when the value is not a filename.
Adds a violation when the value is not a given list.