To define a set of validation rules, you will define a rule set and add rules to the rule set. You then validate data against the rule set, which returns a result set. Each result in the result set contains the value provided, whether or not it is valid, and, if invalid, an error message describing why.
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);
The returned $resultSet
is a Phly\RuleValidation\ResultSet
.
You can check to see if the result set is valid, and pull all values (which returns a map of key/value pairs), and any alidation error messages (returned as a key/message pair):
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();
}