1. Go to this page and download the library: Download lukman-ss/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/ */
lukman-ss / validation example snippets
use Lukman\Validation\Validator;
$data = [
'username' => 'john_doe',
'age' => 25,
];
$rules = [
'username' => 'validated data
$validated = $validator->validated();
} else {
// Get validation errors as a MessageBag
$errors = $validator->errors();
echo $errors->first('username');
}
use Lukman\Validation\Validator;
use Lukman\Validation\Exception\ValidationException;
$validator = new Validator($data, $rules);
try {
$validated = $validator->validate(); // or validateOrFail()
} catch (ValidationException $e) {
$errors = $e->errors(); // MessageBag
print_r($errors->toArray());
}
$validated = $validator->safe(); // Returns validated fields only, without throwing
$data = [
'items' => [
['qty' => 5],
['qty' => 'not-a-number'],
],
];
$rules = [
'items.*.qty' => 'rst('items.1.qty'); // "The items.1.qty must be an integer."
$messages = [
'd' => 'Please provide your email address.',
'items.*.qty.integer' => 'Quantity must be a valid number.',
];
$validator = new Validator($data, $rules, $messages);
use Lukman\Validation\RuleInterface;
class UppercaseRule implements RuleInterface
{
public function passes(string $attribute, mixed $value, array $data): bool
{
return is_string($value) && strtoupper($value) === $value;
}
public function message(string $attribute): string
{
return 'The :attribute must be in uppercase letters.';
}
}
$rules = [
'code' => ['
$rules = [
'username' => [
'xed $value, array $data): bool|string {
if ($value === 'admin') {
return 'You cannot choose "admin" as your username.';
}
return true; // Return true if valid
}
]
];
use Lukman\Validation\ValidatorFactory;
ValidatorFactory::extend('strong_password', function (string $attribute, mixed $value, array $parameters, array $data): bool {
return is_string($value) && strlen($value) >= 8 && preg_match('/[A-Z]/', $value);
}, 'The :attribute must be at least 8 characters long and contain an uppercase letter.');
// Now use it like any standard rule string:
$rules = [
'password' => '
$validator = new Validator($data, $rules);
$validator->after(function (Validator $v) {
if ($v->validated()['password'] === $v->validated()['username']) {
$v->errors()->add('password', 'Password cannot be the same as username.');
}
});
$validator = new Validator($data, $rules);
$validator->passes(); // Runs validation
// Change data and validate again
$validator->setData($newData);
$validator->passes(); // Re-runs validation on new data
// Change rules and validate again
$validator->setRules($newRules);
$validator->passes(); // Re-runs validation on new rules
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.