PHP code example of xtompie / validation

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

    

xtompie / validation example snippets


use Xtompie\Validation\Validation;

$result = Validation::of($input)
    ->key('email')->(fn($input) => $input['email'] != $input['password'])
    ->group()
    ->key('email')->callback(fn($email) => !inUse($email))
    ->result();

Validation::of($input);
Validation::new()->withSubject($input);
Validation::new()->validate($input);

Validation::of($input)
    /* Group 1 */
    ->group()
    /* Group 2 */
    ->group()
    /* Group 3 */
    ;

Validation::new()
    ->main() // validation will target main subject
    ->property($name) // when subject is an object, will target property named $name
    ->method($name) // when subject is an object, will target getter method named $name
    ->key($key) // when subject is an array, will target array value where key is $key
    ->take($callback) // custom target $callback, as first argument main subject will be given
;

    $validation = Validation::of(['person' => ['name' => 'John']])
        ->key('person')
        ->nested()->key('name')->

Validation::new()
    ->key('name')
        ->filter(fn($x) => ucfirst($x)) // custom callback filter
        ->trim()
;

Validation::new()
    ->key('name')->

Validation::new()
    ->key('name')
    // raw validator, validator return Result
    ->validator(fn ($value) => strlen($value) !== 13 ? Result::ofSuccess() : Result::ofErrorMsg('Length can not be 13'))
    // custom callback
    ->callback(fn ($value) => strlen($value) !== 13, 'Length can not be 13')
    ->notBlank('Fill name!')
;

$ok = Validation::of($email)->

$v = Validation::new();
$v->result(); // Xtompie\Result\Result
$v->errors(); // Xtompie\Result\ErrorCollection
$v->error(); // ?Xtompie\Result\Error first error
$v->success(); // bool
$v->fail(); // bool


namespace App\Shared\Validation;

use App\Shared\Dao\Dao;
use Xtompie\Validation\Validation as BaseValidation;

class Validation extends BaseValidation
{
    public function __construct(
        protected Dao $dao,
    ) {}

    protected function msgs(): array
    {
        return array_merge(parent::msgs(), [
            'dao_not_exists' => 'Value {value} already exists',
        ]);
    }

    public function trim(): static
    {
        return $this->filter(fn($v) => trim($v));
    }

    public function digit($msg = 'Only digits allowed', $key = 'digit'): static
    {
        return $this->validator(fn($v) => ctype_digit($v) ? Result::ofSucces() : Result::ofErrorMsg($msg, $key));
    }

    public function daoNotExists(string $table, string $field, ?string $exceptId = null, ?string $msg = null)
    {
        return $this->validator(fn ($v) => $this->test(
            !$this->dao->exists($table, [$field => $v, 'id !=' => $exceptId]),
            'dao_not_exists',
            $msg,
            ['{value}' => $v]
        ));
    }
}

namespace App\User\Application\Service;
use App\Shared\Validation\Validation;

class CreateUserService
{
    public function __construct(
        protected Validation $validation,
    ) {}

    public function __invoke(string $email): Result
    {
        $result = $this->validation->withSubject($email)
            ->