Skip to content
PHP Ecosystem
Explore

BaseFormModel

API reference › Form Model › BaseFormModel

abstract classUIAwesome\FormModel\BaseFormModel
ExtendsUIAwesome\Model\BaseModel
ImplementsFormModelInterface, UIAwesome\Model\ModelInterface
Sourcesrc/BaseFormModel.php

Base implementation of FormModelInterface.

Usage example:

use UIAwesome\FormModel\Attribute\{FieldConfig, Hint, Label, Placeholder};
final class UserForm extends BaseFormModel
{
#[Hint('Use your account email address.')]
#[Label('Email address')]
#[Placeholder('name@example.com')]
#[FieldConfig(['class' => ['w-full rounded-md border border-slate-300 px-3 py-2']])]
public string $email = '';
}
$form = new UserForm();
$label = $form->getLabel('email');
// 'Email address'
MethodDescriptionDefined by
add()Registers a property and its supported types.UIAwesome\Model\BaseModel
addError()Adds an error for a field.BaseFormModel
clearError()Clears errors for one field or all fields.BaseFormModel
getData()Returns the raw input retained after loading.UIAwesome\Model\BaseModel
getError()Returns errors for one field.BaseFormModel
getErrors()Returns validation errors indexed by field name.BaseFormModel
getErrorSummary()Returns a flattened error summary.BaseFormModel
getFieldConfig()Returns the field configuration for one field.BaseFormModel
getFieldConfigs()Returns field configuration arrays indexed by field name.BaseFormModel
getFirstError()Returns the first error for one field.BaseFormModel
getFirstErrors()Returns the first validation error indexed by field name.BaseFormModel
getHint()Returns the hint text for one field.BaseFormModel
getHints()Returns hint text indexed by field name.BaseFormModel
getLabel()Returns the label text for one field.BaseFormModel
getLabels()Returns label text indexed by field name.BaseFormModel
getModelName()Returns the model class name used as the default input scope.UIAwesome\Model\BaseModel
getNames()Returns property names, expanding nested models with dot notation.UIAwesome\Model\BaseModel
getPlaceholder()Returns the placeholder text for one field.BaseFormModel
getPlaceholders()Returns placeholder text indexed by field name.BaseFormModel
getRule()Returns validation rules for one field.BaseFormModel
getRules()Returns validation rules indexed by field name.BaseFormModel
getTypes()Returns collected property types indexed by property name.UIAwesome\Model\BaseModel
getValue()Returns a property value, including nested model paths.UIAwesome\Model\BaseModel
has()Checks whether a property or nested model path exists.UIAwesome\Model\BaseModel
hasError()Checks whether errors exist.BaseFormModel
isEmpty()Checks whether the loaded raw input is empty.UIAwesome\Model\BaseModel
isType()Checks whether a property supports the requested type.UIAwesome\Model\BaseModel
isValidated()Checks whether a field has been validated successfully.BaseFormModel
load()Assigns scoped or unscoped input and retains its raw keys.UIAwesome\Model\BaseModel
setErrors()Replaces all field errors.BaseFormModel
setValue()Assigns a property value using its attributes and type metadata.UIAwesome\Model\BaseModel
setValues()Assigns input values while excluding the selected property names.UIAwesome\Model\BaseModel
toArray()Exports property values with optional top-level snake_case keys.UIAwesome\Model\BaseModel
public function addError(string $field, string $error): void

Adds an error for a field.

Parameters

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

Clears errors for one field or all fields.

Parameters

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

Returns errors for one field.

Parameters

ParameterTypeDescription
$fieldstringField name.

Returns

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

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

Returns validation errors indexed by field name.

Returns

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

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

Returns a flattened error summary.

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.

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.

Parameters

ParameterTypeDescription
$fieldstringField name.

Returns

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

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.

Returns

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

public function getFirstError(string $field): string

Returns the first error for one field.

Parameters

ParameterTypeDescription
$fieldstringField name.

Returns

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

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

Returns the first validation error indexed by field name.

Returns

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

public function getHint(string $field): string

Returns the hint text for one field.

Parameters

ParameterTypeDescription
$fieldstringField name.

Returns

string: Hint text for the field.

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

Returns hint text indexed by field name.

Returns

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

public function getLabel(string $field): string

Returns the label text for one field.

Parameters

ParameterTypeDescription
$fieldstringField name.

Returns

string: Label text for the field.

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

Returns label text indexed by field name.

Returns

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

public function getPlaceholder(string $field): string

Returns the placeholder text for one field.

Parameters

ParameterTypeDescription
$fieldstringField name.

Returns

string: Placeholder text for the field.

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

Returns placeholder text indexed by field name.

Returns

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

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

Returns validation rules for one field.

Parameters

ParameterTypeDescription
$fieldstringField name.

Returns

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

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

Returns validation rules indexed by field name.

Returns

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

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

Checks whether errors exist.

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.

Parameters

ParameterTypeDescription
$fieldstringField name.

Returns

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

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

Replaces all field errors.

Parameters

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