FieldError
API reference › Form Model › FieldError
Stores and queries validation errors by field.
Usage example:
$errors = new FieldError();$errors->add('email', 'Email is invalid.');$firstEmailError = $errors->getField('email', true);Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
add() | Adds an error message for a field. | FieldError |
clear() | Clears errors for one field or all fields. | FieldError |
get() | Returns validation errors indexed by field name. | FieldError |
getAll() | Returns all errors indexed by field name. | FieldError |
getAllFirst() | Returns the first error for each field. | FieldError |
getField() | Returns errors for a single field. | FieldError |
getFirstForField() | Returns the first error for one field. | FieldError |
getForField() | Returns all errors for one field. | FieldError |
getSummary() | Returns a flattened error summary. | FieldError |
has() | Checks whether errors exist. | FieldError |
isValidated() | Checks whether a field has been validated successfully. | FieldError |
set() | Replaces the full error collection. | FieldError |
Method details
Section titled “Method details”add()
Section titled “add() ”public function add(string $field, string $error): voidAdds an error message for a field.
Usage example:
$errors->add('username', 'Username is required.');Parameters
| Parameter | Type | Description |
|---|---|---|
$field | string | Field name. |
$error | string | Error message to append. |
clear()
Section titled “clear() ”public function clear(string|null $field = null): voidClears errors for one field or all fields.
Usage example:
$errors->clear('email');$errors->clear();Parameters
| Parameter | Type | Description |
|---|---|---|
$field | string| | Field name, or null to clear all fields. |
get()
Section titled “get() ”public function get(bool $first = false): array<string, array<int, string>|string>Returns validation errors indexed by field name.
Usage example:
$allErrors = $errors->get();$firstErrors = $errors->get(true);Parameters
| Parameter | Type | Description |
|---|---|---|
$first | bool | Whether to return only the first error for each field. |
Returns
array<string, array<int, string>|string>: Errors indexed by field name. Returns an empty array when no errors exist.
getAll()
Section titled “getAll() ”public function getAll(): array<string, array<int, string>>Returns all errors indexed by field name.
Returns
array<string, array<int, string>>: Non-empty errors indexed by field name.
getAllFirst()
Section titled “getAllFirst() ”public function getAllFirst(): array<string, string>Returns the first error for each field.
Returns
array<string, string>: First errors indexed by field name.
getField()
Section titled “getField() ”public function getField(string $field, bool $first = false): array<int, string>|stringReturns errors for a single field.
Usage example:
$emailErrors = $errors->getField('email');$firstEmailError = $errors->getField('email', true);Parameters
| Parameter | Type | Description |
|---|---|---|
$field | string | Field name. |
$first | bool | Whether to return only the first error. |
Returns
array<int, string>|string: Error list for the field, or the first error when $first is true.
getFirstForField()
Section titled “getFirstForField() ”public function getFirstForField(string $field): stringReturns the first error for one field.
Parameters
| Parameter | Type | Description |
|---|---|---|
$field | string |
Returns
string
getForField()
Section titled “getForField() ”public function getForField(string $field): array<int, string>Returns all errors for one field.
Parameters
| Parameter | Type | Description |
|---|---|---|
$field | string |
Returns
array<int, string>: Errors for the requested field.
getSummary()
Section titled “getSummary() ”public function getSummary(list<string> $onlyFields = [], bool $first = false): array<int|string, string>Returns a flattened error summary.
Usage example:
$summary = $errors->getSummary(['email', 'username']);Parameters
| Parameter | Type | Description |
|---|---|---|
$onlyFields | list<string> | Fields to include. Uses all fields when the list is empty. |
$first | bool | Whether to include only the first error for each field. |
Returns
array<int|string, string>: Flat list of error messages.
has()
Section titled “has() ”public function has(string|null $field = null): boolChecks whether errors exist.
Usage example:
$hasAny = $errors->has();$hasEmailErrors = $errors->has('email');Parameters
| Parameter | Type | Description |
|---|---|---|
$field | string| | Field name, or null to check all fields. |
Returns
bool: true if errors exist, otherwise false.
isValidated()
Section titled “isValidated() ”public function isValidated(string $field): boolChecks whether a field has been validated successfully.
Usage example:
$isValidated = $errors->isValidated('email');Parameters
| Parameter | Type | Description |
|---|---|---|
$field | string | Field name. |
Returns
bool: true if the field exists in the error map with no messages, otherwise false.
set()
Section titled “set() ”public function set(array<string, array<int, string>> $values): voidReplaces the full error collection.
Usage example:
$errors->set(['email' => ['Email is invalid.']]);Parameters
| Parameter | Type | Description |
|---|---|---|
$values | array<string, | Error messages indexed by field name. |