Validation


Validation lets you enforce rules on incoming data (typically $request->all(), see the Request & Response documentation) before acting on it.

Monti validation is handled by the \App\System\Validator\Validator class, a thin wrapper built around somnambulist/validation, a standalone validation library. Rules, available options and error handling are entirely delegated to it, so refer to its documentation for the complete rule list.

Basic usage

$validation = validator($request->all(), [
    "title" => ["required", "string"],
    "age" => ["nullable", "integer"],
    "type" => ["required", "in:post,page"],
]);

if ($validation->success()) {
    // proceed
}

validator() is a shortcut for new \App\System\Validator\Validator($data, $rules).

Checking the result

$validation->success(); // bool, true if all rules passed
$validation->fails();   // bool, the opposite of success()
$validation->failed();  // array of the data that failed validation

Additional rules

On top of every rule provided by the vendor library, Monti registers two extra date rules:

  • before:field_or_date
  • after:field_or_date

before requires the value to be earlier than the target, while after requires it to be later; in both cases the comparison is strict, so the two dates can't be equal. The comparison target can be either a field or a literal date/time string: if a field with that name exists in the same validated data, its value is used; otherwise, the given value is treated as the date itself. The value being validated must be a date that PHP's native DateTime constructor can parse, and since DateTime accepts many formats on its own, pairing the rule with a date:format rule on the same field, as in the example above, additionally requires the value to match that specific format.

validator($request->all(), [
    "start_date" => ["date:Y-m-d H:i", "before:end_date"],
    "end_date" => ["date:Y-m-d H:i", "after:start_date"],
]);

For more details read the somnambulist/validation documentation.