1. Go to this page and download the library: Download selective/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/ */
selective / validation example snippets
use Selective\Validation\Exception\ValidationException;
use Selective\Validation\ValidationResult;
$validationResult = new ValidationResult();
if (empty($data['username'])) {
$validationResult->addError('username', 'Input
if ($validationResult->fails()) {
throw new ValidationException('Please check your input', $validationResult);
}
use Selective\Validation\Exception\ValidationException;
use Selective\Validation\ValidationResult;
// ...
// Get all POST values
$data = (array)$request->getParsedBody();
$validation = new ValidationResult();
// Validate username
if (empty($data['username'])) {
$validation->addError('username', 'Input
use Selective\Validation\ValidationResult;
// Fetch json data from request as array
$jsonData = (array)$request->getParsedBody();
$validation = new ValidationResult();
// ...
if ($validation->fails()) {
throw new ValidationException('Please check your input', $validation);
}
use Selective\Validation\Factory\CakeValidationFactory;
use Selective\Validation\Regex\ValidationRegex;
// ...
$data = [ /* ... */ ];
$validationFactory = new CakeValidationFactory();
$validator = $validationFactory->createValidator();
$validator
->regex('id', ValidationRegex::ID, 'Invalid')
->regex('country', ValidationRegex::COUNTRY_ISO_2, 'Invalid country')
->regex('date_of_birth', ValidationRegex::DATE_DMY, 'Invalid date format');
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Selective\Validation\Encoder\JsonEncoder;
use Selective\Validation\Middleware\ValidationExceptionMiddleware;
use Selective\Validation\Transformer\ErrorDetailsResultTransformer;
use Slim\App;
use Slim\Factory\AppFactory;
// ...
return [
ValidationExceptionMiddleware::class => function (ContainerInterface $container) {
$factory = $container->get(ResponseFactoryInterface::class);
return new ValidationExceptionMiddleware(
$factory,
new ErrorDetailsResultTransformer(),
new JsonEncoder()
);
},
ResponseFactoryInterface::class => function (ContainerInterface $container) {
$app = $container->get(App::class);
return $app->getResponseFactory();
},
App::class => function (ContainerInterface $container) {
AppFactory::setContainer($container);
return AppFactory::create();
},
// ...
];
use Selective\Validation\Middleware\ValidationExceptionMiddleware;
use Slim\Factory\AppFactory;
run();
use Selective\Validation\ValidationException;
use Selective\Validation\ValidationResult;
$validation = new ValidationResult();
// Validate username
if (empty($data->username)) {
$validation->addError('username', 'Input
use Selective\Validation\Factory\CakeValidationFactory;
use Selective\Validation\Exception\ValidationException;
// ...
// Within the Action class: fetch the request data, e.g. from a JSON request
$data = (array)$request->getParsedBody();
// Within the Application Service class: Do the validation
$validationFactory = new CakeValidationFactory();
$validator = $validationFactory->createValidator();
$validator
->notEmptyString('username', 'Input
namespace App\Transformer;
use Selective\Validation\Exception\ValidationException;
use Selective\Validation\Transformer\ResultTransformerInterface;
use Selective\Validation\ValidationResult;
final class MyValidationTransformer implements ResultTransformerInterface
{
public function transform(
ValidationResult $validationResult,
ValidationException $exception = null
): array {
// Implement your own data structure for the response
// ...
return [];
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.