Title here
Summary here
The validate()
method offers flexibility in how validation errors are handled. Here’s how to work with the different
modes.
By default, the first validation failure will throw a PropertyRuleViolation
exception. This is useful for scenarios
where you want immediate feedback and error handling. PropertyRuleViolation
indicates that a specific property has
violated a validation rule.
Example:
use Norvica\Validation\Exception\PropertyRuleViolation;
$data = ['email' => 'john.doe', 'password' => 'P4$$w0rd'];
$rules = ['email' => new Email(), 'password' => new Password()];
try {
$validator->validate($data, $rules);
} catch (PropertyRuleViolation $e) {
$e->getMessage(); // "email: Value must be a valid E-mail address"
$e->getPath(); // "email"
$e->getText(); // "Value must be a valid E-mail address"
}
To collect all validation violations instead of stopping at the first one, pass an Options
instance to
the validate()
method, setting the throw
option to false
.
Example:
use Norvica\Validation\Result;
use Norvica\Validation\Options;
use Norvica\Validation\Exception\PropertyRuleViolation;
$data = ['email' => 'john.doe', 'password' => 'P4$$w0rd'];
$rules = ['email' => new Email(), 'password' => new Password()];
$result = $validator->validate($data, $rules, new Options(throw: false));
if (!empty($result->violations)) {
// handle all errors the way you'd like, for instance:
foreach ($result->violations as $violation) {
echo $violation->getPath() . ": " . $violation->getText() . "\n";
}
}