PHP code example of benycode / slim-request-validation

1. Go to this page and download the library: Download benycode/slim-request-validation 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/ */

    

benycode / slim-request-validation example snippets


use BenyCode\Slim\Validation\Rule\RequestValidationRuleInterface;
use Respect\Validation\Validator;

final class AnyRuleClass implements RequestValidationRuleInterface
{
    public function rules(): array
    {
        return [
          'name' => Validator::alpha(),
          'url' => Validator::filterVar(FILTER_VALIDATE_URL),
        ];
    }
    
    public function messages(): array
    {
        return [
            'filterVar' => '{{name}} must be valid modified message.',
        ];
    }
}

use BenyCode\Slim\Validation\Middleware\RequestValidationExceptionMiddleware;
use BenyCode\Slim\Validation\Transformer\RequestValidatorTransformFactory;
use BenyCode\Slim\Validation\Encoder\JsonEncoder;

$app = new \Slim\App();

$app->post('/api/any_end_point',function ($req, $res, $args) {
 
})
->add(new RequestValidationExceptionMiddleware(new RequestValidatorTransformFactory(), new JsonEncoder()))
->add(new RequestValidation([
	new AnyRuleClass(),
]))	
;

$app->run();

use BenyCode\Slim\Validation\Middleware\RequestValidationExceptionMiddleware;
use BenyCode\Slim\Validation\Transformer\RequestValidatorTransformFactory;
use BenyCode\Slim\Validation\Encoder\JsonEncoder;

$app = new \Slim\App();

$app->post('/api/any_end_point',function ($req, $res, $args) {
 
})
->add(new RequestValidationExceptionMiddleware(new RequestValidatorTransformFactory(), new JsonEncoder()))
->add(new RequestValidation([
	new AnyRuleClass(),
	new AppendedRuleClass(),
	new AppendedRule2Class(),
	new AppendedRule3Class(),
	....
]))	
;

$app->post('/api/any_end_point2',function ($req, $res, $args) {
 
})
->add(new RequestValidationExceptionMiddleware(new RequestValidatorTransformFactory(), new JsonEncoder()))
->add(new RequestValidation([
	new AnyRuleClass(),
	new AppendedRule3Class(),
	....
]))	
;

$app->run();