1. Go to this page and download the library: Download iviphp/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/ */
iviphp / validation example snippets
declare(strict_types=1);
use Ivi\Validation\Rules\Email;
use Ivi\Validation\Rules\Required;
use Ivi\Validation\Rules\StringType;
use Ivi\Validation\Validator;
$result = Validator::make(
data: [
'name' => 'Gaspard',
'email' => '[email protected]',
],
rules: [
'name' => [
new Required(),
new StringType(),
],
'email' => [
new Required(),
new Email(),
],
]
);
if ($result->passes()) {
$validated = $result->validated();
}
if ($result->fails()) {
$errors = $result->errors();
}
declare(strict_types=1);
use Ivi\Validation\Rules\Required;
use Ivi\Validation\Rules\StringType;
use Ivi\Validation\Validator;
$validator = new Validator([
'title' => [
new Required(),
new StringType(),
],
]);
$result = $validator->validate([
'title' => 'My article',
]);
$result = Validator::make(
data: [
'user' => [
'name' => 'Gaspard',
'email' => '[email protected]',
],
],
rules: [
'user.name' => [
new Required(),
new StringType(),
],
'user.email' => [
new Required(),
new Email(),
],
]
);
declare(strict_types=1);
use Ivi\Validation\RuleInterface;
final readonly class StartsWith implements RuleInterface
{
public function __construct(
private string $prefix
) {}
public function passes(
string $field,
mixed $value,
array $data
): bool {
return is_string($value)
&& str_starts_with(
$value,
$this->prefix
);
}
public function message(string $field): string
{
return "The {$field} field must start with {$this->prefix}.";
}
}
final readonly class MatchesField implements RuleInterface
{
public function __construct(
private string $otherField
) {}
public function passes(
string $field,
mixed $value,
array $data
): bool {
return array_key_exists(
$this->otherField,
$data
) && $value === $data[$this->otherField];
}
public function message(string $field): string
{
return "The {$field} field must match {$this->otherField}.";
}
}
declare(strict_types=1);
use Ivi\Validation\Exceptions\ValidationException;
use Ivi\Validation\Rules\Email;
use Ivi\Validation\Rules\In;
use Ivi\Validation\Rules\IntegerType;
use Ivi\Validation\Rules\MaxLength;
use Ivi\Validation\Rules\MinLength;
use Ivi\Validation\Rules\Nullable;
use Ivi\Validation\Rules\Required;
use Ivi\Validation\Rules\StringType;
use Ivi\Validation\Validator;
$data = [
'name' => 'Gaspard Kirira',
'email' => '[email protected]',
'age' => 28,
'role' => 'admin',
'bio' => null,
];
$result = Validator::make(
data: $data,
rules: [
'name' => [
new Required(),
new StringType(),
new MinLength(2),
new MaxLength(100),
],
'email' => [
new Required(),
new Email(),
],
'age' => [
new Required(),
new IntegerType(),
],
'role' => [
new Required(),
new In([
'member',
'admin',
]),
],
'bio' => [
new Nullable(),
new StringType(),
new MaxLength(500),
],
],
stopOnFirstFailure: true
);
if ($result->fails()) {
throw ValidationException::fromResult($result);
}
$validated = $result->validated();