PHP code example of phly / phly-rule-validation
1. Go to this page and download the library: Download phly/phly-rule-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/ */
phly / phly-rule-validation example snippets
use Phly\RuleValidation\Result\Result;
use Phly\RuleValidation\RuleSet\RuleSet;
use Phly\RuleValidation\Rule\CallbackRule;
$rules = new RuleSet();
$rules->add(new CallbackRule('flag', function (mixed $value, array $data): Result {
if (! is_bool($value)) {
return Result::forInvalidValue('flag', $value, 'Not a boolean value');
}
return Result::forValidValue('flag', $value);
}, default: false));
$rules->add(new MyCustomRule());
// and so on
$resultSet = $rules->validate($someFormData);
if ($resultSet->isValid()) {
$values = $resultSet->getValues();
// do something with values
} else {
$messages = $resultSet->getMessages();
// do something with error messages
}
// Get a result for a single key:
$flagResult = $resultSet->flag; // or $resultSet->getResult('flag')
// Get the value from a single result
$flag = $flagResult->value();
// Get the validation status from a single result
if ($flagResult->isValid()) {
// ...
}
// Get an error message for a single result
if (! $flagResult->isValid()) {
echo $flagResult->message();
}