PHP code example of olegkishko / php-field-validator
1. Go to this page and download the library: Download olegkishko/php-field-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/ */
olegkishko / php-field-validator example snippets
$validator
->field('first_name')
->
$validator
->field('password')
-> $value): bool {
return preg_match('/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/', $value);
},
'Password must contain at least one letter and one number',
);
use OlegKishko\PhpFieldValidator\Rules\RuleInterface;
class CustomPasswordRule implements RuleInterface
{
public function validate(mixed $value): bool
{
// Your custom validation logic
return preg_match('/^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$/', $value);
}
public function getErrorMessage(): string
{
return 'Password must contain at least one letter, one number, and one special character';
}
}
// Usage
$validator
->field('password')
->customRule(new CustomPasswordRule());
$validator->validate(['password' => 'simplepassword!']);
$validator->validate($input);
if ($validator->hasErrors()) {
foreach ($validator->getErrors() as $field => $error) {
echo sprintf('[%s]: %s%s', $field, $error, PHP_EOL);
}
}
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.