1. Go to this page and download the library: Download meritum/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/ */
meritum / validation example snippets
use Meritum\Validation\Validator;
$result = $validator->validate(
[
'name' => [' 'password' => ['{
// proceed
}
foreach ($result->getErrors() as $attribute => $messages) {
// $attribute => 'email', $messages => ['The email must be a valid email address']
}
use Meritum\Validation\ValidationModule;
$kernel->addModule(new ValidationModule());
use Meritum\Validation\RuleInterface;
final class Slug implements RuleInterface
{
public function name(): string
{
return 'slug';
}
public function validate(mixed $value, mixed ...$params): bool
{
return is_string($value) && (bool) preg_match('/^[a-z0-9]+(?:-[a-z0-9]+)*$/', $value);
}
public function message(string $attribute, mixed ...$params): string
{
return "The {$attribute} must be a valid slug";
}
}
use Meritum\Validation\ValidationOption;
$kernel->define(Slug::class, fn() => new Slug())->tag(ValidationOption::RuleTag->value);
$validator->validate(['handle' => ['
use Meritum\Validation\Rule;
final class StrictEmail implements RuleInterface
{
public function name(): string
{
return 'email';
}
// ...
}
$kernel->override(Rule\Email::class, fn() => new StrictEmail(), preserve: true);
use Meritum\Validation\StoppableRuleInterface;
final class Bail implements StoppableRuleInterface
{
public function name(): string { return 'bail'; }
public function validate(mixed $value, mixed ...$params): bool { return true; }
public function message(string $attribute, mixed ...$params): string { return ''; }
public function shouldPropagationStop(mixed $value, mixed ...$params): bool
{
return true; // always stop — no further rules run after this
}
}
use Meritum\Validation\FieldReferencingRuleInterface;
use Meritum\Validation\Missing;
final class GreaterThan implements FieldReferencingRuleInterface
{
public function name(): string { return 'greaterThan'; }
public function resolveParams(string $attribute, array $input): array
{
return [$input[$attribute] ?? new Missing(), $attribute];
}
public function validate(mixed $value, mixed ...$params): bool
{
if ($params[0] instanceof Missing) { return false; }
return is_numeric($value) && is_numeric($params[0]) && $value > $params[0];
}
public function message(string $attribute, mixed ...$params): string
{
$field = is_string($params[1]) ? $params[1] : '';
return "The {$attribute} must be greater than {$field}";
}
}
$validator->validate(['end_date' => ['
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.