1. Go to this page and download the library: Download madesimple/slim-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/ */
madesimple / slim-validation example snippets
// Request class
namespace Requests;
class ModelRequestValidation extends \MadeSimple\Slim\Middleware\Validation
{
/**
* @return array Rule set for the request path.
*/
protected function getPathRules(): array
{
return [
// The model locator must be a uuid and pass your custom acl check
'locator' => 'uuid|custom_acl_check'
];
}
protected function getQueryParameterRules(array $routeArguments): array
{
return [];
}
protected function getParsedBodyRules(array $routeArguments): array
{
return [];
}
}
class PaginatedRequestValidation extends \MadeSimple\Slim\Middleware\Validation
{
protected function getPathRules(): array
{
return [];
}
/**
* @param array $routeArguments Route arguments
*
* @return array Rule set for the query parameters.
*/
protected function getQueryParameterRules(array $routeArguments): array
{
return [
// The page must be at least 1
'page' => 'is:int|min:1',
// The items per page must at least 5 but no more than 100
'limit' => 'is:int|min:5|max:100',
];
}
/**
* @param array $routeArguments Route arguments
*
* @return array Rule set for the parsed body.
*/
protected function getParsedBodyRules(array $routeArguments): array
{
return [];
}
}
class PostRequestValidation extends \MadeSimple\Slim\Middleware\Validation
{
protected function getPathRules(): array
{
return [];
}
protected function getQueryParameterRules(array $routeArguments): array
{
return [];
}
/**
* @param array $routeArguments Route arguments
*
* @return array Rule set for the parsed body.
*/
protected function getParsedBodyRules(array $routeArguments): array
{
return [
// The current password is
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
//...
$app = new \Slim\App;
//...
$app->post('/route/path/foo', function (Request $request, Response $response) {
// Validate the request immediately, if it is invalid then it will throw an exception
(new \Request\FooPostValidation($this))->validate($request);
// ...
});