PHP code example of weew / validator

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

    

weew / validator example snippets


$constraint = new EmailConstraint();
// or
$constraint = new EmailConstraint('Custom error message.');
$check = $constraint->check('[email protected]');

if ($check) {
    // valdiation passed
} else {
    echo $constraint->getMessage();
}

$group = new ConstraintGroup('email', [
    new EmailConstraint(),
]);
// or
$group = new ConstraintGroup('email');
$group->addConstraint(new EmailConstraint());

$result = $group->check('[email protected]');

$validator = new Validator();
$data = ['username' => 'foo', 'email' => '[email protected]'];

$result = $validator->check($data, [
    new ConstraintGroup('email', [
        new EmailConstraint(),
    ]),
]);

if ($result->isFailed()) {
    foreach ($result->getErrors() as $error) {
        // $error->getSubject()
        // $error->getValue()
        // $error->getMessage()
        // $error->getConstraint()
    }
}

$data = ['username' => 'foo', 'email' => '[email protected]'];
$validator->addConstraint('email', new EmailConstraint());
$validator->addConstraints('username', [
    new AlphaConstraint(),
    new MinMaxLengthConstraint(3, 20),
]);

$result = $validator->check($data);

class UserProfileValidator extends Validator {
    protected function configure() {
        $this->addConstraint('email', new EmailConstraint());
        $this->addConstraints('username', [
            new AlphaConstraint(),
            new MinMaxLengthConstraint(3, 20),
        ]);
    }
}

$data = ['username' => 'foo', 'email' => '[email protected]'];
$validator = new UserProfileValidator();
$result = $validator->check($data);

class MinMaxConstraint implements IConstraint {
    protected $min;
    protected $max;
    protected $message;

    public function __construct($min, $max, $message = null) {
        $this->min = $min;
        $this->max = $max;
        $this->message = $message;
    }

    public function check($value, IValidationData $data = null) {
        if (is_numeric($value)) {
            return $value >= $this->min && $value <= $this->max;
        }

        return false;
    }

    public function getMessage() {
        if ($this->message !== null) {
            return $this->message;
        }

        return 'Some default error message.';
    }

    public function getOptions() {
        return [
            'min' => $this->min,
            'max' => $this->max,
        ];
    }
}

$input = [
    'items' => [
        ['name' => 'name1'],
        ['name' => null],
        ['name' => 'name3'],
    ],
];

$result = $validator->addConstraint('items.*.name', new NotNullConstraint());

$input = [
    'items' => [
        'name1' => 'value1',
        '2' => 'value2',
        'name3' => 'value3',
    ],
];

$result = $validator->addConstraint('items.#', new MinMaxLengthConstraint(3, 5));

$data = ['email' => '[email protected]'];
$validator->check($data);

$data = new stdClass();
$data->email = '[email protected]';
$validator->check($data);

class User {
    protected $username;
    public function __construct($username) {
        $this->username = $username;
    }
    public function getUsername() {
        return $this->username;
    }
}

$validator->check(new User('foo'));