PHP code example of timefrontiers / php-validator
1. Go to this page and download the library: Download timefrontiers/php-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/ */
timefrontiers / php-validator example snippets
use TimeFrontiers\Validation\BulkValidationResult;
use TimeFrontiers\Validation\ValidationConfigurationException;
use TimeFrontiers\Validation\ValidationException;
use TimeFrontiers\Validation\ValidationResult;
use TimeFrontiers\Validation\Validator;
$result = Validator::field('email', $_POST['email'] ?? null)
-> $result->value();
} else {
$message = $result->first('email');
}
$result = Validator::field('username', $value)
->bail(false)
->alpha()->message('Use letters only.')
->min(3)
->validate();
$result = Validator::field('identifier', $value)
->custom(static function (mixed $value): array {
if (!is_string($value) || !str_starts_with($value, 'TF-')) {
return [false, null, 'Invalid identifier.'];
}
return [true, strtoupper($value), null];
})
->validate();
$result = Validator::make($_POST, [
'name' => 'red|int:18,120',
'status' => 'in:active,inactive,pending',
'website' => 'nullable|url',
]);
$result = Validator::make($data, [
'age' => [''in', ['active', 'inactive'], true]],
'name' => [['name', ['root', 'admin'], 2, 35]],
]);
$result = Validator::make($data, [
'age' => [
'
$result = Validator::make($data, [
'code' => ' 'escaped' => 'pattern:/a\/[b|c]{1,3}/i',
]);
$result = Validator::make(
['user' => ['email' => '[email protected] ']],
['user.email' => '
$result->passes();
$result->fails();
$result->isValid();
$result->value();
$result->field();
$result->errors();
$result->errorsFor('email');
$result->hasError('email');
$result->first('email');
$result->messages();
$result->errorCount();
$result->throwIfFailed();
$result->toArray(); // valid, value, field, errors
$result->passes();
$result->fails();
$result->isValid();
$result->validated();
$result->get('email', $default);
$result->errors();
$result->errorsFor('email');
$result->hasError('email');
$result->first('email');
$result->messages();
$result->errorCount();
$result->throwIfFailed();
$result->toArray(); // valid, validated, errors
$result = Validator::make(
$data,
['email' => 's.'],
);
try {
$data = Validator::validate($_POST, ['email' => ' // Developer/configuration defect: fix the policy.
} catch (ValidationException $exception) {
$errors = $exception->errors();
$first = $exception->first();
}
bash
composer