BaseFormModel
API reference › Form Model › BaseFormModel
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'Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
add() | Registers a property and its supported types. | UIAwesome\ |
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\ |
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\ |
getNames() | Returns property names, expanding nested models with dot notation. | UIAwesome\ |
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\ |
getValue() | Returns a property value, including nested model paths. | UIAwesome\ |
has() | Checks whether a property or nested model path exists. | UIAwesome\ |
hasError() | Checks whether errors exist. | BaseFormModel |
isEmpty() | Checks whether the loaded raw input is empty. | UIAwesome\ |
isType() | Checks whether a property supports the requested type. | UIAwesome\ |
isValidated() | Checks whether a field has been validated successfully. | BaseFormModel |
load() | Assigns scoped or unscoped input and retains its raw keys. | UIAwesome\ |
setErrors() | Replaces all field errors. | BaseFormModel |
setValue() | Assigns a property value using its attributes and type metadata. | UIAwesome\ |
setValues() | Assigns input values while excluding the selected property names. | UIAwesome\ |
toArray() | Exports property values with optional top-level snake_case keys. | UIAwesome\ |
Method details
Section titled “Method details”addError()
Section titled “addError() ”public function addError(string $field, string $error): voidAdds an error for a field.
Parameters
| Parameter | Type | Description |
|---|---|---|
$field | string | Field name. |
$error | string | Error message to append. |
clearError()
Section titled “clearError() ”public function clearError(string|null $field = null): voidClears errors for one field or all fields.
Parameters
| Parameter | Type | Description |
|---|---|---|
$field | string| | Field name, or null to clear all fields. |
getError()
Section titled “getError() ”public function getError(string $field): array<int, string>Returns errors for one field.
Parameters
| Parameter | Type | Description |
|---|---|---|
$field | string | Field name. |
Returns
array<int, string>: Error list for the field.
getErrors()
Section titled “getErrors() ”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.
getErrorSummary()
Section titled “getErrorSummary() ”public function getErrorSummary(list<string> $onlyFields = [], bool $first = false): array<int|string, string>Returns a flattened error summary.
Parameters
| Parameter | Type | Description |
|---|---|---|
$onlyFields | list<string> | Fields to include. Uses all fields when the list is empty. |
$first | bool | Whether 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.
getFieldConfig()
Section titled “getFieldConfig() ”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
| Parameter | Type | Description |
|---|---|---|
$field | string | Field name. |
Returns
array<string, mixed>: Field configuration for the field. Returns an empty array when the field has no configuration.
getFieldConfigs()
Section titled “getFieldConfigs() ”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.
getFirstError()
Section titled “getFirstError() ”public function getFirstError(string $field): stringReturns the first error for one field.
Parameters
| Parameter | Type | Description |
|---|---|---|
$field | string | Field name. |
Returns
string: First error for the field, or an empty string when no errors exist.
getFirstErrors()
Section titled “getFirstErrors() ”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.
getHint()
Section titled “getHint() ”public function getHint(string $field): stringReturns the hint text for one field.
Parameters
| Parameter | Type | Description |
|---|---|---|
$field | string | Field name. |
Returns
string: Hint text for the field.
getHints()
Section titled “getHints() ”public function getHints(): array<string, string>Returns hint text indexed by field name.
Returns
array<string, string>: Hint text indexed by field name.
getLabel()
Section titled “getLabel() ”public function getLabel(string $field): stringReturns the label text for one field.
Parameters
| Parameter | Type | Description |
|---|---|---|
$field | string | Field name. |
Returns
string: Label text for the field.
getLabels()
Section titled “getLabels() ”public function getLabels(): array<string, string>Returns label text indexed by field name.
Returns
array<string, string>: Label text indexed by field name.
getPlaceholder()
Section titled “getPlaceholder() ”public function getPlaceholder(string $field): stringReturns the placeholder text for one field.
Parameters
| Parameter | Type | Description |
|---|---|---|
$field | string | Field name. |
Returns
string: Placeholder text for the field.
getPlaceholders()
Section titled “getPlaceholders() ”public function getPlaceholders(): array<string, string>Returns placeholder text indexed by field name.
Returns
array<string, string>: Placeholder text indexed by field name.
getRule()
Section titled “getRule() ”public function getRule(string $field): array<mixed, mixed>|nullReturns validation rules for one field.
Parameters
| Parameter | Type | Description |
|---|---|---|
$field | string | Field name. |
Returns
array<mixed, mixed>|null: Validation rules for the field, or null when no rules are defined.
getRules()
Section titled “getRules() ”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.
hasError()
Section titled “hasError() ”public function hasError(string|null $field = null): boolChecks whether errors exist.
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.
Parameters
| Parameter | Type | Description |
|---|---|---|
$field | string | Field name. |
Returns
bool: true if the field exists in the error map with no messages, otherwise false.
setErrors()
Section titled “setErrors() ”public function setErrors(array<string, array<int, string>> $values): voidReplaces all field errors.
Parameters
| Parameter | Type | Description |
|---|---|---|
$values | array<string, | Error messages indexed by field name. |