Validate input
Declare field rules, handle failures, and understand the returned data.
On this page
Declare field rules
Validation is available independently of a controller:
use Anvyr\Loom\Validation\Validator;
$data = Validator::make(
['email' => 'reader@example.com', 'name' => 'Reader'],
['email' => 'required|email', 'name' => 'required|min:2|max:80'],
)->validate();
Rules can be pipe-separated strings or lists of strings. required checks empty values. Other rules skip empty values, so combine them with required when a field must be present.
Handle a validation failure
use Anvyr\Loom\Exceptions\ValidationException;
try {
$data = Validator::make(['email' => 'invalid'], [
'email' => 'required|email',
])->validate();
} catch (ValidationException $exception) {
$errors = $exception->getErrors();
}
Errors are grouped by field, with a list of messages for each field. The exception's HTTP response uses status 422, with JSON for a request that expects JSON and an HTML error response otherwise.
Understand the result
validate() returns input keys that have declared rules. It does not turn the result into a typed DTO or automatically cast values to match a rule. Validate first, then explicitly convert values needed by your application.
For nested field rules, review the returned structure before treating it as a complete sanitized payload: the implementation selects top-level input keys by rule key.
Choose rules from the implementation
Built-in rules include email, url, numeric, integer, boolean, alpha, alphanumeric, in, regex, same, different, date, and array, alongside length checks. Custom rules use the validation extension registry.
Use Validator as the reference for exact accepted values. Input validation does not replace authorization, CSRF protection, or output escaping.