Skip to main content

Validation

Validate request input with $request->validate(). It returns only the fields that have rules and answers 422 when anything fails:

$data = $request->validate([
'email' => 'required|email|max:255',
'name' => 'required|string|between:2,100',
'age' => 'nullable|int|between:13,130',
'role' => 'in:admin,editor,viewer',
'tags' => 'array|max:10',
]);
{
"error": {
"status": 422,
"message": "The given data was invalid",
"errors": { "email": ["The email must be a valid email address."] }
},
"request_id": "6b6a7a2277941ec27ba5e964acef9f2a"
}

Input is read from the JSON body, then form fields, then the query string (see $request->input()).

Rules​

RulePasses when
requiredpresent and not empty
nullablethe field may be null (other rules are skipped for null)
string, int, numeric, bool, arrayvalue has that type (int accepts integer strings)
emaila valid e-mail address
urlan http or https URL
uuidan RFC 4122 UUID
min:N, max:Nstring length, array size or numeric value is at least / at most N
between:A,Bas min and max together
in:a,b,cone of the listed values
regex:/pattern/matches the pattern

Rules are separated by |. Fields without required are optional: their rules only apply when they are present.

Outside a request​

use Omnitrix\Validation\Validator;
use Omnitrix\Validation\ValidationException;

try {
$clean = (new Validator(['email' => 'required|email']))->validate($input);
} catch (ValidationException $e) {
$errors = $e->details()['errors'];
}

Mass assignment​

Models only accept attributes listed in their $fillable property from create(), fill() and update(), so passing validated input straight to a model cannot set columns such as id or is_admin unless you allow it. See Models.