Docs LATEST

Validation Rules

Built-in validation rules with examples.

Validation

VelvetCMS includes common validation rules out of the box. Rules can be combined using the pipe (|) separator.

Rule Reference #

required #

Field must be present and non-empty.

'email' => 'required'

Empty strings, null values, and empty arrays fail this rule.

email #

Field must be a valid email address.

'email' => 'required|email'

Uses PHP's FILTER_VALIDATE_EMAIL.

url #

Field must be a valid URL.

'website' => 'url'

Uses PHP's FILTER_VALIDATE_URL.

min #

Minimum length (strings) or count (arrays).

'password' => 'required|min:8'        // At least 8 characters
'tags' => 'array|min:1'               // At least 1 item

max #

Maximum length (strings) or count (arrays).

'title' => 'required|max:200'         // No more than 200 characters
'files' => 'array|max:10'             // No more than 10 items

numeric #

Field must be numeric (integers, floats, numeric strings).

'price' => 'required|numeric'

integer #

Field must be an integer.

'quantity' => 'required|integer'
'age' => 'integer|min:0|max:120'

boolean #

Field must be a boolean-like value.

'active' => 'boolean'

Accepts: true, false, 1, 0, "1", "0", "true", "false"

alpha #

Field may only contain letters (a-z, A-Z).

'name' => 'required|alpha'

alphanumeric #

Field may only contain letters and numbers.

'username' => 'required|alphanumeric|min:3|max:20'

in #

Field must be one of the listed values.

'status' => 'required|in:draft,published,archived'
'role' => 'in:admin,editor,viewer'

same #

Field must match another field's value.

'password_confirmation' => 'same:password'

different #

Field must be different from another field's value.

'new_password' => 'required|different:current_password'

date #

Field must be a parseable date string.

'published_at' => 'date'
'birth_date' => 'required|date'

Uses PHP's strtotime() for parsing.

regex #

Field must match the given regular expression.

'slug' => 'required|regex:/^[a-z0-9-]+$/'
'phone' => 'regex:/^\+?[0-9]{10,14}$/'

The pattern must be a complete PHP regex including delimiters.

array #

Field must be an array.

'items' => 'required|array'
'tags' => 'array|min:1|max:10'

Combining Rules #

Chain multiple rules with pipes:

$rules = [
    'email' => 'required|email|max:255',
    'password' => 'required|min:8|max:100',
    'status' => 'required|in:draft,published',
    'tags' => 'array|max:5',
];

Rules run in order. If required fails, subsequent rules are skipped for that field.

Optional Fields #

Fields without required are optional. If the field is missing or empty, other rules are skipped:

$rules = [
    'nickname' => 'max:50',           // Optional, but if provided, max 50 chars
    'website' => 'url',               // Optional, but if provided, must be valid URL
];

Complete Example #

use VelvetCMS\Validation\Validator;

$data = $request->all();

$rules = [
    'title' => 'required|max:200',
    'slug' => 'required|alphanumeric|max:100',
    'content' => 'required',
    'status' => 'required|in:draft,published',
    'author_email' => 'required|email',
    'tags' => 'array|max:10',
    'publish_date' => 'date',
];

try {
    $validated = Validator::make($data, $rules)->validate();
    // $validated contains only the validated fields
} catch (ValidationException $e) {
    // $e->errors() returns ['field' => ['error message', ...], ...]
}

Error Messages #

Each rule generates a descriptive error message:

Rule Message
required The {field} field is required.
email The {field} must be a valid email address.
min The {field} must be at least {n} characters.
max The {field} may not be greater than {n} characters.
in The selected {field} is invalid.
same The {field} must match {other}.
regex The {field} format is invalid.

What's Next #