Skip to content

Latest commit

 

History

History
70 lines (52 loc) · 1.82 KB

usage.md

File metadata and controls

70 lines (52 loc) · 1.82 KB

Basic Usage

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.

Defining a RuleSet

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

Validating a RuleSet

$resultSet = $rules->validate($someFormData);

The returned $resultSet is a Phly\RuleValidation\ResultSet.

Working with Validation Results

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
}

Working with individual results

// 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();
}