PHP code example of phpdot / validator

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

    

phpdot / validator example snippets


use PHPdot\Validator\Rule\Email;
use PHPdot\Validator\Rule\Min;
use PHPdot\Validator\Rule\Required;
use PHPdot\Validator\ValidatorFactory;

$validator = (new ValidatorFactory())->create();

$errors = $validator->validate($request->all(), [
    'username' => [
        (new Required())->withError(UserErrorCode::UsernameRequired),
        (new Min(3))->withError(UserErrorCode::UsernameTooShort),
    ],
    'email' => [
        (new Required())->withError(UserErrorCode::EmailRequired),
        (new Email())->withError(UserErrorCode::EmailInvalid),
    ],
]);

if ($errors->hasErrors()) {
    return $response->json($errors->toArray(), $errors->getHttpStatus());
}

'username_too_short' => 'The username must be at least {min} characters.',

(new ClosureRule($check))->withError(UserErrorCode::PrefixInvalid, ['min' => 1, 'max' => 4]),
(new Between(1, 999))->withError(UserErrorCode::CodeRange, ['min' => 1, 'max' => 999]),

public function __construct(private readonly ValidatorFactory $validators) {}

$bag = $this->validators->create()->validate($input, $rules);

$v = $this->validators->create();
$v->validate($userInput, $userRules);
$v->validate($paymentInput, $paymentRules);
$bag = $v->errors();   // both payloads' errors combined