PHP code example of inok / slim-validation4

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

    

inok / slim-validation4 example snippets


use Respect\Validation\Validator as v;

$app = new \Slim\App();

//Create the validators
$usernameValidator = v::alnum()->noWhitespace()->length(1, 10);
$ageValidator = v::numericVal()->positive()->between(1, 20);
$validators = [
  'username' => $usernameValidator,
  'age' => $ageValidator
];

$app->get('/api/myEndPoint',function ($req, $res, $args) {
    //Here you expect 'username' and 'age' parameters
    if($req->getAttribute('has_errors')){
      //There are errors, read them
      $errors = $req->getAttribute('errors');

      /* $errors contains:
      [
        'username' => [
          'length' => '"davidepastore" must have a length between 1 and 10',
        ],
        'age' => [
          'between' => '"89" must be between 1 and 20',
        ],
      ];
      */
    } else {
      //No errors
    }

})->add(new \Inok\Slim\Validation\Validation($validators));

$app->run();

use Respect\Validation\Validator as v;

$app = new \Slim\App();

//Create the validators
$usernameValidator = v::alnum()->noWhitespace()->length(1, 10);
$ageValidator = v::numericVal()->positive()->between(1, 20);
$validators = [
  'username' => $usernameValidator,
  'age' => $ageValidator
];

// Register middleware for all routes
// If you are implementing per-route checks you must not add this
$app->add(new \Inok\Slim\Validation\Validation($validators));

$app->get('/foo', function ($req, $res, $args) {
  //Here you expect 'username' and 'age' parameters
  if($req->getAttribute('has_errors')){
    //There are errors, read them
    $errors = $req->getAttribute('errors');

    /* $errors contains:
    [
      'username' => [
        'length' => '"davidepastore" must have a length between 1 and 10',
      ],
      'age' => [
        'between' => '"89" must be between 1 and 20',
      ],
    ];
    */
  } else {
    //No errors
  }
});

$app->post('/bar', function ($req, $res, $args) {
  //Here you expect 'username' and 'age' parameters
  if($req->getAttribute('has_errors')){
    //There are errors, read them
    $errors = $req->getAttribute('errors');
  } else {
    //No errors
  }
});

$app->run();

use Respect\Validation\Validator as v;

$app = new \Slim\App();

//Create the validators
$routeParamValidator = v::numericVal()->positive();
$validators = [
  'param' => $routeParamValidator,
];

$app->get('/foo/{param}', function ($req, $res, $args) {
  //Here you expect 'param' route parameter
  if($req->getAttribute('has_errors')){
    //There are errors, read them
    $errors = $req->getAttribute('errors');

    /* $errors contains:
    [
        'param' => [
          'numeric' => '"wrong" must be numeric',
        ],
    ];
    */
  } else {
    //No errors
  }
})->add(new \Inok\Slim\Validation\Validation($validators));

$app->run();

use Respect\Validation\Validator as v;

$app = new \Slim\App();

//Create the validators
$typeValidator = v::alnum()->noWhitespace()->length(3, 5);
$emailNameValidator = v::alnum()->noWhitespace()->length(1, 2);
$validators = [
  'type' => $typeValidator,
  'email' => [
    'name' => $emailNameValidator,
  ],
];

//In your route
$errors = $req->getAttribute('errors');

print_r($errors);
/*
Array
(
    [email.name] => Array
        (
            'length' => "rq3r" must have a length between 1 and 2
        )

)
*/

use Respect\Validation\Validator as v;

$app = new \Slim\App();

//Create the validators
$typeValidator = v::alnum()->noWhitespace()->length(3, 5);
$emailNameValidator = v::alnum()->noWhitespace()->length(1, 2);
$validators = [
  'type' => $typeValidator,
  'email' => [
    'name' => $emailNameValidator,
  ],
];

//In your route
$errors = $req->getAttribute('errors');

print_r($errors);
/*
Array
(
    [email.name] => Array
        (
            'length' => "rq3r" must have a length between 1 and 2
        )

)
*/

use Respect\Validation\Validator as v;

$app = new \Slim\App();

//Create the validators
$usernameValidator = v::alnum()->noWhitespace()->length(1, 10);
$ageValidator = v::numericVal()->positive()->between(1, 20);
$validators = [
  'username' => $usernameValidator,
  'age' => $ageValidator
];

$translator = function($message){
  $messages = [
      'These rules must pass for {{name}}' => 'Queste regole devono passare per {{name}}',
      '{{name}} must be a string' => '{{name}} deve essere una stringa',
      '{{name}} must have a length between {{minValue}} and {{maxValue}}' => '{{name}} deve avere una dimensione di caratteri compresa tra {{minValue}} e {{maxValue}}',
  ];
  return $messages[$message];
};

$middleware = new \Inok\Slim\Validation\Validation($validators, $translator);

// Register middleware for all routes or only for one...

$app->run();