Download the PHP package pedrohenriques/data-validator without Composer

On this page you can find all versions of the php package pedrohenriques/data-validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package data-validator

PHP Data Validator - Easy to Use and Customizable

A PHP library that allows for an easy and customizable way to validate data.

The code will run through an array of data and for each item will execute the specified validations. In the end any validations that failed will generate an error message that you can display.

All the validations and error messages can be customized, even the default ones, and you can create new ones "on the fly" very easily.

Instructions

Setup

=> Composer:

Add the following to your project's composer.json file:

Replacing the version with your preference.

=> Manual:

  1. Copy the src folder into your project.
  2. Reference the DataValidator.php file on the webpages you wish to use the Data Validator using, for example, require_once("path/to/DataValidator.php").


Using the Data Validator

In order to use the Data Validator create an instance of the DataValidator class using the code:

With:


Once the class has been instantiated, the code $validator->validate(array $data, array $validations[, $single_fail]); will execute the validations and generate any error messages.

With:


=> $validations parameter syntax:

The complete syntax is $validations = ["field;alias" => ["check_type1;value1;value2...", "check_type2;value1;value2...", ...], ...] where:


=> Default Validations:

This repository comes with the following validations available:

Check Type Description Values
date Checks if the $data entry is a valid date, according to any of the PHP accepted formats none
date_f Checks if the $data entry is a valid date, according to specific formats the accepted formats separated by ;

The formats should follow PHP's date format
email Checks if the $data entry is a valid email format none
gr_eq_th Checks if the $data entry is greater or equal than the validation value (as floats) the number representing the floor for the check
gr_th Checks if the $data entry is greater than the validation value (as floats) the number representing the floor for the check
in Checks if the $data entry is one of the validation values (as strings) the desired values separated by ;
lw_eq_th Checks if the $data entry is lower or equal than the validation value (as floats) the number representing the ceiling for the check
lw_th Checks if the $data entry is lower than the validation value (as floats) the number representing the ceiling for the check
match Checks if the $data entry matches the validation value (as strings) The value to match against
match_ci Checks if the $data entry matches (case insensitive) the validation value (as strings) The value to match against
max_len Checks if the $data entry has a maximum length of the validation value (as integers) the number representing the maximum length
min_len Checks if the $data entry has a minimum length of the validation value (as integers) the number representing the minimum length
range Checks if the $data entry is between the lower and upper bounds of the validation values, including the bounds (as floats) the lower bound followed by the upper bound, separated by ;
regex Checks if the $data entry matches the pattern given as validation value. the pattern (ex: /^[a-z]+$/)
required Checks if the $data entry exists and is not an empty string none


=> Example:

Given the inputs:

and the validations:

the following errors will be generated:


=> Available Methods:

Assuming $validator contains an instance of the DataValidator class.

The following methods allow for the execution of the validations and access to their results:


=> Basic Methods

Method Description Parameters Use
validate Executes all the validations and creates any error messages array with the values to validate

array with the validation configuration

boolean controlling whether 1 failed check stops the rest from being executed, for each field
$validator->validate($data, $validations[, $single_fail]);

For further details, consult the topic Using the Data Validator above
passed Returns True if all validations for all fields passed and False if at least 1 validation failed. none $validator->passed();
failed Returns True if at least 1 validation failed and False if all validations for all fields passed. none $validator->failed();
getErrors Returns the requested error(s) message(s). string with the field name

integer with the error index
$validator->getErrors([string $field, integer $index]);

If just field is provided, then all errors for that field will be returned.
If no parameters are provided, then all errors for all fields will be returned.


=> Advanced Methods

NOTE: These methods are used to customize the Data Validator. For further detail consult the topic Customizing the Data Validator below.

Method Description Parameters Use
addValidation Registers a temporary validation string with the check type

function with the validation code
$validator->addValidation(string $check_type, callable $function)
addShortCircuit Registers a new short-circuit rule string with the check

array with the relevant $input values

boolean to flag if the check should be executed
$validator->addShortCircuit(string $check, array $input_values, boolean $run_check)
addMessage Registers an error message string with the message

string with the check type for this message

string with the field name for this message
$validator->addMessage(string $message, string $check_type[, string $field])

If $field is not provided, the error message will be applied to the provided check type for all fields.
getDebugErrors Returns an array with any debug messages generated after calling validate() none $validator->getDebugErrors();

These include any validation classes not found, invalid syntax for the $validations array, among others.


Customizing the Data Validator

It's possible to customize the validations as well as the error messages in 2 ways:

  1. Persistent: Adding a new validation as a class and an error message as an entry in the error_msg.json file.
  2. Temporary: Registering a new validation as a temporary function and an error message as a temporary string.


Validations:

When a validation is called, the code will first look for a temporary function with the same check type. If no matching function can be found the code will look for a defined class with a matching name (see below for naming convention).
This means that custom temporary validations will have priority over the persistent validations. If you wish to change the behavior of a persistent validation, register a temporary function with the same check type.





Examples:

Given the following code:

The information available to the validation method/function is:



Error Messages:

When an error message is generated, the code will first look for a temporary string with the same check type and field. If no matching string can be found the code will look for the persistent message with a matching name.
This means that custom temporary strings will have priority over the persistent messages. If you wish to change the content of a persistent message, register a temporary string with the same check type.





Examples:

Given the following code:

The information available to the error messages is:



Short-Circuit Rules:

It's possible to configure certain combinations of check types and input values that will "short-circuit" the rest of the validations, preventing them from being executed.

In order to register a temporary short-circuit rule use the method addShortCircuit, listed above as an advanced method.

The permanent short-circuit combinations are defined in the $short_circuit_rules variable, located at the start of the DataValidator class' definition.

By default this variable has the following value:

The first entry reads as follows: "if the field being validated has the required check and the $data entry is either null or "", then execute the required check but no others."
This prevents multiple error messages from being generated and checks being executed unnecessarily since there is no input value to validate and one was required.
Note that by still executing the required check, which will fail, there will still be an error message being generated for that check.

The second entry reads as follows: "if the field being validated doesn't have the required check and the $data entry is either null or "", then don't execute the required check or any other checks."
This prevents an optional field that was left empty from being validated and potentially generating error messages, when no validation is required.
However, if the field was provided ($input will be a non empty string) then any relevant checks will be executed.
Note that an ! is used to denote the absence of a check.


All versions of data-validator with dependencies

PHP Build Version
Package Version
Requires php Version >=5.4
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package pedrohenriques/data-validator contains the following files

Loading the files please wait ....