Skip to content
PHP Ecosystem
Explore

FieldError

API reference › Form Model › FieldError

final classUIAwesome\FormModel\FieldError
Sourcesrc/FieldError.php

Stores and queries validation errors by field.

Usage example:

$errors = new FieldError();
$errors->add('email', 'Email is invalid.');
$firstEmailError = $errors->getField('email', true);
MethodDescriptionDefined 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
public function add(string $field, string $error): void

Adds an error message for a field.

Usage example:

$errors->add('username', 'Username is required.');

Parameters

ParameterTypeDescription
$fieldstringField name.
$errorstringError message to append.
public function clear(string|null $field = null): void

Clears errors for one field or all fields.

Usage example:

$errors->clear('email');
$errors->clear();

Parameters

ParameterTypeDescription
$fieldstring|nullField name, or null to clear all fields.
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

ParameterTypeDescription
$firstboolWhether 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.

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.

public function getAllFirst(): array<string, string>

Returns the first error for each field.

Returns

array<string, string>: First errors indexed by field name.

public function getField(string $field, bool $first = false): array<int, string>|string

Returns errors for a single field.

Usage example:

$emailErrors = $errors->getField('email');
$firstEmailError = $errors->getField('email', true);

Parameters

ParameterTypeDescription
$fieldstringField name.
$firstboolWhether to return only the first error.

Returns

array<int, string>|string: Error list for the field, or the first error when $first is true.

public function getFirstForField(string $field): string

Returns the first error for one field.

Parameters

ParameterTypeDescription
$fieldstring

Returns

string

public function getForField(string $field): array<int, string>

Returns all errors for one field.

Parameters

ParameterTypeDescription
$fieldstring

Returns

array<int, string>: Errors for the requested field.

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

ParameterTypeDescription
$onlyFieldslist<string>Fields to include. Uses all fields when the list is empty.
$firstboolWhether to include only the first error for each field.

Returns

array<int|string, string>: Flat list of error messages.

public function has(string|null $field = null): bool

Checks whether errors exist.

Usage example:

$hasAny = $errors->has();
$hasEmailErrors = $errors->has('email');

Parameters

ParameterTypeDescription
$fieldstring|nullField name, or null to check all fields.

Returns

bool: true if errors exist, otherwise false.

public function isValidated(string $field): bool

Checks whether a field has been validated successfully.

Usage example:

$isValidated = $errors->isValidated('email');

Parameters

ParameterTypeDescription
$fieldstringField name.

Returns

bool: true if the field exists in the error map with no messages, otherwise false.

public function set(array<string, array<int, string>> $values): void

Replaces the full error collection.

Usage example:

$errors->set(['email' => ['Email is invalid.']]);

Parameters

ParameterTypeDescription
$valuesarray<string, array<int, string>>Error messages indexed by field name.