PHP code example of gd-75 / request-body-validator

1. Go to this page and download the library: Download gd-75/request-body-validator 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/ */

    

gd-75 / request-body-validator example snippets


$pb = $request->getParsedBody();
if(
    isset($pb["field"], $pb["field1"], $pb["field2"],  $pb["field3"],  $pb["field4"])
    && !empty($pb["field"]) && !empty($pb["field2"])
    && is_numeric($pb["field3"])
){
    // Do stuff
}

$validator = new RequestBodyValidator($request->getParsedBody());
if(
    $validator->validateMultiple(["field1", "field4"], RequestBodyValidator::EXISTS)
    && $validator->validateMultiple(["field", "field2"], RequestBodyValidator::NOT_EMPTY)
    && $validator->validateOne("field3", RequestBodyValidator::NUMERIC)
){
    // Do stuff
}

// Constructor, $request is your PSR-7 request object
$validator = new RequestBodyValidator($request->getParsedBody());

// validateOne, validates a single field
$validator->validateOne($name, $criteria);

// validateMultiple, validates multiple fields with the same criteria
$validator->validateMultiple([$name, $name1], $criteria);