PHP code example of alexpts / simple-validator

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

    

alexpts / simple-validator example snippets


$body = (array)$this->request->getParsedBody();

$validator = new Validator;

// shot format
$errors = $validator->validate($body, [
    'parentId' => ['int', 'betweenInt:0:99999'],
    'name' => ['string', 'min:3'],
    'slug' => ['string', 'min:3', 'max:120'],
    'sort' => ['int']
]);

// full format
$errors = $validator->validate($body, [
    'parentId' => ['int', ['betweenInt' => [0, 99999]] ],
    'name' => ['string', ['min' => [3]] ],
    'slug' => ['string', ['min' => [3]], ['max' => [120]] ],
    'sort' => ['int']
]);

// validate if exists
$errors = $validator->validateIfExists($body, [
    'name' => ['string', ['min' => [3]] ],
    'sort' => ['int'] // field not passed, not validate
]);

// deep attributes
$errors = $validator->validate($body, [
    'user' => ['array', 'min:3'],
    'user.name' => ['string', 'min:3'],
    'user.age' => ['int'],
]);

$errors = $validator->validateIfExists($body, [
    'user' => ['array', 'min:3'],
    'user.name' => ['string', 'min:3'],
    'user.age' => ['int'],
]);

public function registerRule(string $name, callable $handler): self