PHP code example of jessesoeteman / validate-get-post-input

1. Go to this page and download the library: Download jessesoeteman/validate-get-post-input library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jessesoeteman / validate-get-post-input example snippets




use ValidateGetPostInput\Prebuilt\ValidateBoolean;

$_boolean = new ValidateBoolean("boolean"); // The name of the input, so $_GET["boolean"] or $_POST["boolean"]
$errors += $_boolean->validate(); // This returns an array with errors if there are any, returns an empty array if there are no errors.
$boolean = $_boolean->getValue(); // This returns the value of the input.

use ValidateGetPostInput\Prebuilt\ValidateBoolean;

$_boolean = new ValidateBoolean("boolean", false);
$errors += $_boolean->validate(); 
$boolean = $_boolean->getValue();

use ValidateGetPostInput\Prebuilt\ValidateNumber;

$_number = new ValidateNumber("number"); // The name of the input, so $_GET["number"] or $_POST["number"]
$errors += $_number->validate(); // This returns an array with errors if there are any, returns an empty array if there are no errors.
$number = $_number->getValue(); // This returns the value of the input.

use ValidateGetPostInput\Prebuilt\ValidateFloat;

$_float = new ValidateFloat("float"); // The name of the input, so $_GET["float"] or $_POST["float"]
$errors += $_float->validate(); // This returns an array with errors if there are any, returns an empty array if there are no errors.
$float = $_float->getValue(); // This returns the value of the input.

use ValidateGetPostInput\Prebuilt\ValidateText;

$_text = new ValidateText("text"); // The name of the input, so $_GET["text"] or $_POST["text"]
$errors += $_text->validate(); // This returns an array with errors if there are any, returns an empty array if there are no errors.
$text = $_text->getValue(); // This returns the value of the input.

use ValidateGetPostInput\Prebuilt\ValidateJSON;

$_json = new ValidateJSON("json"); // The name of the input, so $_GET["json"] or $_POST["json"]
$errors += $_json->validate(); // This returns an array with errors if there are any, returns an empty array if there are no errors.
$json = $_json->getValue(); // This returns the value of the input.

use ValidateGetPostInput\Prebuilt\ValidateID;

$_id = new ValidateID("id"); // The name of the input, so $_GET["id"] or $_POST["id"]
$errors += $_id->validate(); // This returns an array with errors if there are any, returns an empty array if there are no errors.
$id = $_id->getValue(); // This returns the value of the input.

use ValidateGetPostInput\Prebuilt\ValidateEmail;

$_email = new ValidateEmail("email"); // The name of the input, so $_GET["email"] or $_POST["email"]
$errors[] = $_email->validate(); // This returns an array with errors if there are any, returns an empty array if there are no errors.
$email = $_email->getValue(); // This returns the value of the input.

use ValidateGetPostInput\Prebuilt\ValidateVarchar255;

$_varchar255 = new ValidateVarchar255("varchar255"); // The name of the input, so $_GET["varchar255"] or $_POST["varchar255"]
$errors += $_varchar255->validate(); // This returns an array with errors if there are any, returns an empty array if there are no errors.
$varchar255 = $_varchar255->getValue(); // This returns the value of the input.

use ValidateGetPostInput\Prebuilt\ValidateVarchar255Regex;

$_varchar255Regex = new ValidateVarchar255Regex("varchar255Regex", "/^[a-zA-Z0-9]+$/"); // The name of the input, so $_GET["varchar255Regex"] or $_POST["varchar255Regex"], the second parameter is the regex, the third parameter is if the input is 

use ValidateGetPostInput\Prebuilt\ValidateDate;
use ValidateGetPostInput\Statics\DateFormat;

$_date = new ValidateDate("date", true, DateFormat::YYYY_MM_DD); // The name of the input, so $_GET["date"] or $_POST["date"], the second parameter is if the input is 

use ValidateGetPostInput\ValidateMultiple;

use ValidateGetPostInput\Prebuilt\ValidateID;
use ValidateGetPostInput\Prebuilt\ValidateVarchar255;
use ValidateGetPostInput\Prebuilt\ValidateText;


try {
    $validations = new ValidateMultiple([
        new ValidateID("id"),
        new ValidateVarchar255("name"),
        new ValidateText("description"),
        new ValidateVarchar255("link")
    ]);
} catch (Exception $e) {
    $erros += $e->getMessage();
    // Do something with the errors
    exit();
}
$error += $validations->validate();
// Check if the validation faid
if (count($errors) > 0) {
    // Do something with the errors
    exit();
}
// Get the validated data as an key value array
$inputData = $validations->getValues();