Documentation

ValidationException extends LwtException

Exception thrown for input validation errors.

Supports both single-field and multi-field validation errors with structured error messages for API responses.

Tags
since
3.0.0

Table of Contents

Properties

$context  : array<string, mixed>
Additional context data for logging/debugging.
$errors  : array<string, array<string|int, string>>
Validation errors by field.
$field  : string|null
The field that caused the validation error (for single-field errors).
$httpStatusCode  : int
HTTP status code to return (for web responses).
$shouldLog  : bool
Whether this exception should be logged.

Methods

__construct()  : mixed
Create a new validation exception.
addError()  : self
Add an error for a field.
entityNotFound()  : self
Create exception for entity not found during validation.
forField()  : self
Create exception for a single field validation error.
getContext()  : array<string, mixed>
Get the context data for this exception.
getErrors()  : array<string, array<string|int, string>>
Get all validation errors.
getField()  : string|null
Get the field name (for single-field errors).
getFieldErrors()  : array<string|int, string>
Get errors for a specific field.
getFirstError()  : string|null
Get the first error message.
getHttpStatusCode()  : int
Get the HTTP status code for this exception.
getUserMessage()  : string
Get a user-friendly message for display.
hasFieldError()  : bool
Check if a field has errors.
invalidEmail()  : self
Create exception for invalid email.
invalidEnum()  : self
Create exception for invalid enum value.
invalidLength()  : self
Create exception for string length violation.
invalidType()  : self
Create exception for an invalid type.
invalidUrl()  : self
Create exception for invalid URL.
outOfRange()  : self
Create exception for a value out of allowed range.
requiredField()  : self
Create exception for a required field that is missing.
setHttpStatusCode()  : self
Set the HTTP status code.
shouldLog()  : bool
Whether this exception should be logged.
toApiResponse()  : array{message: string, errors: array}
Convert to array format suitable for JSON API responses.
toArray()  : array<string, mixed>
Convert exception to array for logging/JSON responses.
withContext()  : self
Add context data to the exception.
withErrors()  : self
Create exception for multiple validation errors.

Properties

$context

Additional context data for logging/debugging.

protected array<string, mixed> $context = []

$errors

Validation errors by field.

protected array<string, array<string|int, string>> $errors = []

$field

The field that caused the validation error (for single-field errors).

protected string|null $field = null

$httpStatusCode

HTTP status code to return (for web responses).

protected int $httpStatusCode = 500

$shouldLog

Whether this exception should be logged.

protected bool $shouldLog = true

Methods

__construct()

Create a new validation exception.

public __construct([string $message = 'Validation failed' ][, array<string, array<string|int, string>> $errors = [] ][, int $code = 0 ][, Throwable|null $previous = null ]) : mixed
Parameters
$message : string = 'Validation failed'

The exception message

$errors : array<string, array<string|int, string>> = []

Validation errors by field

$code : int = 0

The exception code

$previous : Throwable|null = null

Previous exception

addError()

Add an error for a field.

public addError(string $field, string $message) : self
Parameters
$field : string

Field name

$message : string

Error message

Return values
self

entityNotFound()

Create exception for entity not found during validation.

public static entityNotFound(string $field, string $entityType, int|string $id) : self
Parameters
$field : string

Field name

$entityType : string

Entity type (e.g., "language", "text")

$id : int|string

The ID that was not found

Return values
self

forField()

Create exception for a single field validation error.

public static forField(string $field, string $message[, mixed $value = null ]) : self
Parameters
$field : string

Field name

$message : string

Error message

$value : mixed = null

The invalid value (optional, for context)

Return values
self

getContext()

Get the context data for this exception.

public getContext() : array<string, mixed>
Return values
array<string, mixed>

getErrors()

Get all validation errors.

public getErrors() : array<string, array<string|int, string>>
Return values
array<string, array<string|int, string>>

getField()

Get the field name (for single-field errors).

public getField() : string|null
Return values
string|null

getFieldErrors()

Get errors for a specific field.

public getFieldErrors(string $field) : array<string|int, string>
Parameters
$field : string

Field name

Return values
array<string|int, string>

getFirstError()

Get the first error message.

public getFirstError() : string|null
Return values
string|null

getHttpStatusCode()

Get the HTTP status code for this exception.

public getHttpStatusCode() : int
Return values
int

getUserMessage()

Get a user-friendly message for display.

public getUserMessage() : string
Return values
string

hasFieldError()

Check if a field has errors.

public hasFieldError(string $field) : bool
Parameters
$field : string

Field name

Return values
bool

invalidEmail()

Create exception for invalid email.

public static invalidEmail(string $field, string $value) : self
Parameters
$field : string

Field name

$value : string

The invalid email

Return values
self

invalidEnum()

Create exception for invalid enum value.

public static invalidEnum(string $field, array<string|int, scalar> $allowed, mixed $value) : self
Parameters
$field : string

Field name

$allowed : array<string|int, scalar>

Allowed values

$value : mixed

The invalid value

Return values
self

invalidLength()

Create exception for string length violation.

public static invalidLength(string $field, int|null $minLength, int|null $maxLength, int $actual) : self
Parameters
$field : string

Field name

$minLength : int|null

Minimum length

$maxLength : int|null

Maximum length

$actual : int

Actual length

Return values
self

invalidType()

Create exception for an invalid type.

public static invalidType(string $field, string $expected, string $actual) : self
Parameters
$field : string

Field name

$expected : string

Expected type

$actual : string

Actual type received

Return values
self

invalidUrl()

Create exception for invalid URL.

public static invalidUrl(string $field, string $value) : self
Parameters
$field : string

Field name

$value : string

The invalid URL

Return values
self

outOfRange()

Create exception for a value out of allowed range.

public static outOfRange(string $field, int|null $min, int|null $max, mixed $value) : self
Parameters
$field : string

Field name

$min : int|null

Minimum allowed value

$max : int|null

Maximum allowed value

$value : mixed

The actual value

Return values
self

requiredField()

Create exception for a required field that is missing.

public static requiredField(string $field) : self
Parameters
$field : string

Field name

Return values
self

setHttpStatusCode()

Set the HTTP status code.

public setHttpStatusCode(int $code) : self
Parameters
$code : int

HTTP status code

Return values
self

shouldLog()

Whether this exception should be logged.

public shouldLog() : bool
Return values
bool

toApiResponse()

Convert to array format suitable for JSON API responses.

public toApiResponse() : array{message: string, errors: array}
Return values
array{message: string, errors: array}

toArray()

Convert exception to array for logging/JSON responses.

public toArray([bool $includeTrace = false ]) : array<string, mixed>
Parameters
$includeTrace : bool = false

Whether to include stack trace

Return values
array<string, mixed>

withContext()

Add context data to the exception.

public withContext(string $key, mixed $value) : self
Parameters
$key : string

Context key

$value : mixed

Context value

Return values
self

withErrors()

Create exception for multiple validation errors.

public static withErrors(array<string, array<string|int, string>> $errors) : self
Parameters
$errors : array<string, array<string|int, string>>

Errors by field

Return values
self

        
On this page

Search results