Skip to content
PHP Ecosystem
Explore

FormModelInterface

API reference › Form Model › FormModelInterface

interfaceUIAwesome\FormModel\FormModelInterface
ExtendsUIAwesome\Model\ModelInterface
Sourcesrc/FormModelInterface.php
Implemented by 1 type

Defines the contract for form models with validation metadata and field errors.

Usage example:

final class UserForm extends BaseFormModel
{
public string $email = '';
}
$form = new UserForm();
$form->addError('email', 'Email is invalid.');
MethodDescriptionDefined by
add() abstractAdds a new property to the model.UIAwesome\Model\ModelInterface
addError() abstractAdds an error for a field.FormModelInterface
clearError() abstractClears errors for one field or all fields.FormModelInterface
getData() abstractReturns the raw data for the model.UIAwesome\Model\ModelInterface
getError() abstractReturns errors for one field.FormModelInterface
getErrors() abstractReturns validation errors indexed by field name.FormModelInterface
getErrorSummary() abstractReturns a flattened error summary.FormModelInterface
getFieldConfig() abstractReturns the field configuration for one field.FormModelInterface
getFieldConfigs() abstractReturns field configuration arrays indexed by field name.FormModelInterface
getFirstError() abstractReturns the first error for one field.FormModelInterface
getFirstErrors() abstractReturns the first validation error indexed by field name.FormModelInterface
getHint() abstractReturns the hint text for one field.FormModelInterface
getHints() abstractReturns hint text indexed by field name.FormModelInterface
getLabel() abstractReturns the label text for one field.FormModelInterface
getLabels() abstractReturns label text indexed by field name.FormModelInterface
getModelName() abstractReturns the model name that this model class should use.UIAwesome\Model\ModelInterface
getNames() abstractReturns the list of property names.UIAwesome\Model\ModelInterface
getPlaceholder() abstractReturns the placeholder text for one field.FormModelInterface
getPlaceholders() abstractReturns placeholder text indexed by field name.FormModelInterface
getRule() abstractReturns validation rules for one field.FormModelInterface
getRules() abstractReturns validation rules indexed by field name.FormModelInterface
getTypes() abstractReturns the list of property types indexed by property names.UIAwesome\Model\ModelInterface
getValue() abstractReturns the value (raw data) for the specified property.UIAwesome\Model\ModelInterface
has() abstractChecks if the model has the specified property.UIAwesome\Model\ModelInterface
hasError() abstractChecks whether errors exist.FormModelInterface
isEmpty() abstractWhether the model has no loaded raw data.UIAwesome\Model\ModelInterface
isType() abstractChecks if the property is of the specified type.UIAwesome\Model\ModelInterface
isValidated() abstractChecks whether a field has been validated successfully.FormModelInterface
load() abstractPopulates the model with input data.UIAwesome\Model\ModelInterface
setErrors() abstractReplaces all field errors.FormModelInterface
setValue() abstractSets the value for the specified property.UIAwesome\Model\ModelInterface
setValues() abstractSets values for multiple properties.UIAwesome\Model\ModelInterface
toArray() abstractReturns model properties as an array.UIAwesome\Model\ModelInterface
abstract public function addError(string $field, string $error): void

Adds an error for a field.

Usage example:

$form->addError('email', 'Email is invalid.');

Parameters

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

Clears errors for one field or all fields.

Usage example:

$form->clearError('email');
$form->clearError();

Parameters

ParameterTypeDescription
$fieldstring|nullField name, or null to clear all fields.
abstract public function getError(string $field): array<int, string>

Returns errors for one field.

Usage example:

$errors = $form->getError('email');

Parameters

ParameterTypeDescription
$fieldstringField name.

Returns

array<int, string>: Error list for the field.

abstract public function getErrors(): array<string, array<int, string>>

Returns validation errors indexed by field name.

Usage example:

$allErrors = $form->getErrors();

Returns

array<string, array<int, string>>: Errors indexed by field name. Returns an empty array when no errors exist.

abstract public function getErrorSummary(list<string> $onlyFields = [], bool $first = false): array<int|string, string>

Returns a flattened error summary.

Usage example:

$summary = $form->getErrorSummary(['email', 'username']);

Parameters

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

Returns

array<int|string, string>: Flat list of error messages when $first is false; field-indexed first errors when $first is true.

abstract public function getFieldConfig(string $field): array<string, mixed>

Returns the field configuration for one field.

A field configuration maps a method name to the argument, or list of arguments, applied to the field or to the widget that renders it.

Usage example:

$emailFieldConfig = $form->getFieldConfig('email');
// ['class' => ['w-full rounded-md']]

Parameters

ParameterTypeDescription
$fieldstringField name.

Returns

array<string, mixed>: Field configuration for the field. Returns an empty array when the field has no configuration.

abstract public function getFieldConfigs(): array<string, array<string, mixed>>

Returns field configuration arrays indexed by field name.

Each configuration maps a method name to the argument, or list of arguments, applied to the field or to the widget that renders it.

Usage example:

$fieldConfig = $form->getFieldConfigs();
// ['email' => ['class' => ['w-full rounded-md']]]

Returns

array<string, array<string, mixed>>: Field configuration arrays indexed by field name.

abstract public function getFirstError(string $field): string

Returns the first error for one field.

Usage example:

$firstError = $form->getFirstError('email');

Parameters

ParameterTypeDescription
$fieldstringField name.

Returns

string: First error for the field, or an empty string when no errors exist.

abstract public function getFirstErrors(): array<string, string>

Returns the first validation error indexed by field name.

Usage example:

$firstErrors = $form->getFirstErrors();

Returns

array<string, string>: First errors indexed by field name. Returns an empty array when no errors exist.

abstract public function getHint(string $field): string

Returns the hint text for one field.

Usage example:

$hint = $form->getHint('email');

Parameters

ParameterTypeDescription
$fieldstringField name.

Returns

string: Hint text for the field.

abstract public function getHints(): array<string, string>

Returns hint text indexed by field name.

Usage example:

$hints = $form->getHints();

Returns

array<string, string>: Hint text indexed by field name.

abstract public function getLabel(string $field): string

Returns the label text for one field.

Usage example:

$label = $form->getLabel('email');

Parameters

ParameterTypeDescription
$fieldstringField name.

Returns

string: Label text for the field.

abstract public function getLabels(): array<string, string>

Returns label text indexed by field name.

Usage example:

$labels = $form->getLabels();

Returns

array<string, string>: Label text indexed by field name.

abstract public function getPlaceholder(string $field): string

Returns the placeholder text for one field.

Usage example:

$placeholder = $form->getPlaceholder('email');

Parameters

ParameterTypeDescription
$fieldstringField name.

Returns

string: Placeholder text for the field.

abstract public function getPlaceholders(): array<string, string>

Returns placeholder text indexed by field name.

Usage example:

$placeholders = $form->getPlaceholders();

Returns

array<string, string>: Placeholder text indexed by field name.

abstract public function getRule(string $field): array<mixed, mixed>|null

Returns validation rules for one field.

Usage example:

$emailRules = $form->getRule('email');

Parameters

ParameterTypeDescription
$fieldstringField name.

Returns

array<mixed, mixed>|null: Validation rules for the field, or null when no rules are defined.

abstract public function getRules(): iterable<string, array<mixed, mixed>>

Returns validation rules indexed by field name.

Usage example:

$rules = $form->getRules();

Returns

iterable<string, array<mixed, mixed>>: Validation rules indexed by field name.

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

Checks whether errors exist.

Usage example:

$hasAnyError = $form->hasError();
$hasEmailError = $form->hasError('email');

Parameters

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

Returns

bool: true if errors exist, otherwise false.

abstract public function isValidated(string $field): bool

Checks whether a field has been validated successfully.

Usage example:

$isValid = $form->isValidated('email');

Parameters

ParameterTypeDescription
$fieldstringField name.

Returns

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

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

Replaces all field errors.

Usage example:

$form->setErrors(['email' => ['Email is invalid.']]);

Parameters

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