ShortCircuit()ShortCircuit(Validator ...$validators)
Validates the input against a series of validators, stopping at the first failure.
Like PHP's && operator, it uses short-circuit evaluation: once the outcome is determined, remaining validators are
skipped. Unlike AllOf, which evaluates all validators and collects all failures, ShortCircuit returns
immediately.
v::shortCircuit(v::intVal(), v::positive())->assert(15);
// Validation passes successfullyThis is useful when:
- You want only the first error message instead of all of them
- Later validators depend on earlier ones passing (e.g., checking a format before checking a value)
- You want to avoid unnecessary validation work
This validator is particularly useful in combination with Factory when later validations depend on earlier results. For example, validating a subdivision code that depends on a valid country code:
$validator = v::shortCircuit(
v::key('countryCode', v::countryCode()),
v::factory(static fn($input) => v::key('subdivisionCode', v::subdivisionCode($input['countryCode']))),
);
$validator->assert([]);
// → `.countryCode` must be present
$validator->assert(['countryCode' => 'US']);
// → `.subdivisionCode` must be present
$validator->assert(['countryCode' => 'US', 'subdivisionCode' => 'ZZ']);
// → `.subdivisionCode` must be a subdivision code of United States
$validator->assert(['countryCode' => 'US', 'subdivisionCode' => 'CA']);
// Validation passes successfullyBecause SubdivisionCode requires a valid country code, it only makes sense to validate the
subdivision after the country code passes. You could achieve this with When, but you would have to repeat
v::key('countryCode', v::countryCode()) twice.
The check() method in ValidatorBuilder uses ShortCircuit internally to short-circuit the entire validation chain. Use ShortCircuit directly when you need fine-grained control over which specific group of validators should fail fast, while letting the rest of the validation continue collecting errors via assert().
This validator does not have templates of its own. It returns the result of the first failing validator, or the result of the last validator when all pass.
- Composite
- Conditions
- Nesting
| Version | Description |
|---|---|
| 3.0.0 | Created |