The validation layer uses a fluent API instead of Laravel-style rule strings.
Available facade:
Validator
The validation layer gives you:
ValidationManagerto create validatorsValidatorto coordinate multiple fieldsFieldValidatorfor fluent rule configuration per fieldValidationExceptionfor throw-on-failure workflows
use Myxa\Validation\ValidationManager;
$validator = (new ValidationManager())->make([
'email' => 'john@example.com',
'user_id' => 1,
]);Then configure rules field by field:
$validator->field('email')
->required('Please provide an email address.')
->string()
->email()
->max(255);
$validator->field('user_id')
->required()
->integer();$validator = (new ValidationManager())->make([
'name' => 'John',
'email' => 'john@example.com',
'notes' => null,
]);
$validator->field('name')->required()->string()->min(2)->max(50);
$validator->field('email')->required()->string()->email()->max(255);
$validator->field('notes')->nullable()->string();
if ($validator->fails()) {
$errors = $validator->errors();
} else {
$validated = $validator->validated();
}Or throw on failure:
$validated = $validator->validate();required()nullable()
Behavior:
- missing fields fail only when
required()is configured nullable()allows an explicitnullvalue- when a field is
nullandnullable()is set, the remaining rules for that field are skipped - nested fields can be targeted with dot paths such as
user.email - array items can be targeted with
*wildcards such astags.*
string()integer()numeric()boolean()array()email()
min($value)max($value)
The size meaning depends on the value type:
- strings use string length
- arrays use item count
- numeric values use their numeric value
Use dot notation to validate nested input:
$validator = (new ValidationManager())->make([
'user' => [
'name' => 'John',
'roles' => ['admin', 'editor'],
],
]);
$validator->field('user.name')->required()->string()->min(2);
$validator->field('user.roles')->required()->array()->min(1);Use * to validate each array item:
$validator->field('user.roles.*')->required()->string()->min(3);When wildcard validation fails, errors are keyed by the concrete item path:
[
'user.roles.1' => [
'The user.roles.1 field must be a string.',
],
]exists() can validate against several sources:
- a SQL model class
- a Mongo model class
- an array of allowed values
- a custom callable returning
trueorfalse
$validator->field('user_id')
->required()
->integer()
->exists(User::class);You can also validate against a specific SQL model column:
$validator->field('email')
->required()
->string()
->exists(User::class, 'email');$validator->field('document_id')
->required()
->exists(UserDocument::class);For Mongo models, exists() only supports the model primary key.
$validator->field('role')->exists(['admin', 'editor']);$validator->field('code')->exists(
static fn (mixed $value): bool => in_array($value, ['A', 'B'], true),
);Each rule accepts an optional custom message:
- string message
- callable message
String example:
$validator->field('name')->required('Name is mandatory.');Callable example:
$validator->field('email')->email(
static fn (mixed $value, string $field): string => sprintf(
'%s "%s" is invalid.',
$field,
(string) $value,
),
);validated() and validate() return only the configured fields that are present in the input data.
$validator = (new ValidationManager())->make([
'name' => 'John',
'email' => 'john@example.com',
'ignored' => 'value',
]);
$validator->field('name')->required()->string();
$validator->field('email')->required()->string()->email();
$validated = $validator->validate();Result:
[
'name' => 'John',
'email' => 'john@example.com',
]if ($validator->fails()) {
$errors = $validator->errors();
}Error format:
[
'email' => [
'The email field must be a valid email address.',
],
]In application code, register ValidationServiceProvider to expose the shared manager and initialize the facade:
use Myxa\Application;
use Myxa\Validation\ValidationServiceProvider;
$app = new Application();
$app->register(new ValidationServiceProvider());
$app->boot();Then use the facade:
use Myxa\Support\Facades\Validator;
$validator = Validator::make([
'name' => 'John',
'email' => 'john@example.com',
]);
$validator->field('name')->required()->string();
$validator->field('email')->required()->string()->email();validate() throws ValidationException when validation fails:
use Myxa\Validation\Exceptions\ValidationException;
try {
$validated = $validator->validate();
} catch (ValidationException $exception) {
$errors = $exception->errors();
}- the API is fluent and field-oriented rather than string-rule oriented
passes()andfails()evaluate all configured fields and collect grouped errorsvalidate()throws, whilevalidated()returns the validated subset after a successful validation passexists()supports SQL models, Mongo models, arrays of allowed values, and custom callables