# BaseFormModel

> Base implementation of UIAwesome\FormModel\FormModelInterface.

[API reference](/docs/api/index.md) › [Form Model](/docs/extensions/form-model/reference/index.md) › BaseFormModel

| abstract class | `UIAwesome\FormModel\BaseFormModel` |
| --- | --- |
| Extends | [`UIAwesome\Model\BaseModel`](/docs/extensions/model/reference/basemodel/index.md) |
| Implements | [`FormModelInterface`](/docs/extensions/form-model/reference/formmodelinterface/index.md), [`UIAwesome\Model\ModelInterface`](/docs/extensions/model/reference/modelinterface/index.md) |
| Source | [`src/BaseFormModel.php`](https://github.com/ui-awesome/form-model/blob/main/src/BaseFormModel.php#L41) |

Base implementation of [`FormModelInterface`](/docs/extensions/form-model/reference/formmodelinterface/index.md).

Usage example:
```php
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

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-add"></span>[`add()`](/docs/extensions/model/reference/basemodel/index.md#method-add) | Registers a property and its supported types. | [`UIAwesome\Model\BaseModel`](/docs/extensions/model/reference/basemodel/index.md) |
| [`addError()`](#method-addError) | Adds an error for a field. | `BaseFormModel` |
| [`clearError()`](#method-clearError) | Clears errors for one field or all fields. | `BaseFormModel` |
| <span id="method-getData"></span>[`getData()`](/docs/extensions/model/reference/basemodel/index.md#method-getData) | Returns the raw input retained after loading. | [`UIAwesome\Model\BaseModel`](/docs/extensions/model/reference/basemodel/index.md) |
| [`getError()`](#method-getError) | Returns errors for one field. | `BaseFormModel` |
| [`getErrors()`](#method-getErrors) | Returns validation errors indexed by field name. | `BaseFormModel` |
| [`getErrorSummary()`](#method-getErrorSummary) | Returns a flattened error summary. | `BaseFormModel` |
| [`getFieldConfig()`](#method-getFieldConfig) | Returns the field configuration for one field. | `BaseFormModel` |
| [`getFieldConfigs()`](#method-getFieldConfigs) | Returns field configuration arrays indexed by field name. | `BaseFormModel` |
| [`getFirstError()`](#method-getFirstError) | Returns the first error for one field. | `BaseFormModel` |
| [`getFirstErrors()`](#method-getFirstErrors) | Returns the first validation error indexed by field name. | `BaseFormModel` |
| [`getHint()`](#method-getHint) | Returns the hint text for one field. | `BaseFormModel` |
| [`getHints()`](#method-getHints) | Returns hint text indexed by field name. | `BaseFormModel` |
| [`getLabel()`](#method-getLabel) | Returns the label text for one field. | `BaseFormModel` |
| [`getLabels()`](#method-getLabels) | Returns label text indexed by field name. | `BaseFormModel` |
| <span id="method-getModelName"></span>[`getModelName()`](/docs/extensions/model/reference/basemodel/index.md#method-getModelName) | Returns the model class name used as the default input scope. | [`UIAwesome\Model\BaseModel`](/docs/extensions/model/reference/basemodel/index.md) |
| <span id="method-getNames"></span>[`getNames()`](/docs/extensions/model/reference/basemodel/index.md#method-getNames) | Returns property names, expanding nested models with dot notation. | [`UIAwesome\Model\BaseModel`](/docs/extensions/model/reference/basemodel/index.md) |
| [`getPlaceholder()`](#method-getPlaceholder) | Returns the placeholder text for one field. | `BaseFormModel` |
| [`getPlaceholders()`](#method-getPlaceholders) | Returns placeholder text indexed by field name. | `BaseFormModel` |
| [`getRule()`](#method-getRule) | Returns validation rules for one field. | `BaseFormModel` |
| [`getRules()`](#method-getRules) | Returns validation rules indexed by field name. | `BaseFormModel` |
| <span id="method-getTypes"></span>[`getTypes()`](/docs/extensions/model/reference/basemodel/index.md#method-getTypes) | Returns collected property types indexed by property name. | [`UIAwesome\Model\BaseModel`](/docs/extensions/model/reference/basemodel/index.md) |
| <span id="method-getValue"></span>[`getValue()`](/docs/extensions/model/reference/basemodel/index.md#method-getValue) | Returns a property value, including nested model paths. | [`UIAwesome\Model\BaseModel`](/docs/extensions/model/reference/basemodel/index.md) |
| <span id="method-has"></span>[`has()`](/docs/extensions/model/reference/basemodel/index.md#method-has) | Checks whether a property or nested model path exists. | [`UIAwesome\Model\BaseModel`](/docs/extensions/model/reference/basemodel/index.md) |
| [`hasError()`](#method-hasError) | Checks whether errors exist. | `BaseFormModel` |
| <span id="method-isEmpty"></span>[`isEmpty()`](/docs/extensions/model/reference/basemodel/index.md#method-isEmpty) | Checks whether the loaded raw input is empty. | [`UIAwesome\Model\BaseModel`](/docs/extensions/model/reference/basemodel/index.md) |
| <span id="method-isType"></span>[`isType()`](/docs/extensions/model/reference/basemodel/index.md#method-isType) | Checks whether a property supports the requested type. | [`UIAwesome\Model\BaseModel`](/docs/extensions/model/reference/basemodel/index.md) |
| [`isValidated()`](#method-isValidated) | Checks whether a field has been validated successfully. | `BaseFormModel` |
| <span id="method-load"></span>[`load()`](/docs/extensions/model/reference/basemodel/index.md#method-load) | Assigns scoped or unscoped input and retains its raw keys. | [`UIAwesome\Model\BaseModel`](/docs/extensions/model/reference/basemodel/index.md) |
| [`setErrors()`](#method-setErrors) | Replaces all field errors. | `BaseFormModel` |
| <span id="method-setValue"></span>[`setValue()`](/docs/extensions/model/reference/basemodel/index.md#method-setValue) | Assigns a property value using its attributes and type metadata. | [`UIAwesome\Model\BaseModel`](/docs/extensions/model/reference/basemodel/index.md) |
| <span id="method-setValues"></span>[`setValues()`](/docs/extensions/model/reference/basemodel/index.md#method-setValues) | Assigns input values while excluding the selected property names. | [`UIAwesome\Model\BaseModel`](/docs/extensions/model/reference/basemodel/index.md) |
| <span id="method-toArray"></span>[`toArray()`](/docs/extensions/model/reference/basemodel/index.md#method-toArray) | Exports property values with optional top-level snake_case keys. | [`UIAwesome\Model\BaseModel`](/docs/extensions/model/reference/basemodel/index.md) |

## Method details

### addError() <span id="method-addError"></span>

```php
public function addError(string $field, string $error): void
```

Adds an error for a field.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$field` | `string` | Field name. |
| `$error` | `string` | Error message to append. |

### clearError() <span id="method-clearError"></span>

```php
public function clearError(string|null $field = null): void
```

Clears errors for one field or all fields.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$field` | `string\|null` | Field name, or `null` to clear all fields. |

### getError() <span id="method-getError"></span>

```php
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() <span id="method-getErrors"></span>

```php
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() <span id="method-getErrorSummary"></span>

```php
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() <span id="method-getFieldConfig"></span>

```php
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() <span id="method-getFieldConfigs"></span>

```php
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() <span id="method-getFirstError"></span>

```php
public function getFirstError(string $field): string
```

Returns 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() <span id="method-getFirstErrors"></span>

```php
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() <span id="method-getHint"></span>

```php
public function getHint(string $field): string
```

Returns the hint text for one field.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$field` | `string` | Field name. |

**Returns**

`string`: Hint text for the field.

### getHints() <span id="method-getHints"></span>

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

Returns hint text indexed by field name.

**Returns**

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

### getLabel() <span id="method-getLabel"></span>

```php
public function getLabel(string $field): string
```

Returns the label text for one field.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$field` | `string` | Field name. |

**Returns**

`string`: Label text for the field.

### getLabels() <span id="method-getLabels"></span>

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

Returns label text indexed by field name.

**Returns**

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

### getPlaceholder() <span id="method-getPlaceholder"></span>

```php
public function getPlaceholder(string $field): string
```

Returns the placeholder text for one field.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$field` | `string` | Field name. |

**Returns**

`string`: Placeholder text for the field.

### getPlaceholders() <span id="method-getPlaceholders"></span>

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

Returns placeholder text indexed by field name.

**Returns**

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

### getRule() <span id="method-getRule"></span>

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

Returns 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() <span id="method-getRules"></span>

```php
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() <span id="method-hasError"></span>

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

Checks whether errors exist.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$field` | `string\|null` | Field name, or `null` to check all fields. |

**Returns**

`bool`: `true` if errors exist, otherwise `false`.

### isValidated() <span id="method-isValidated"></span>

```php
public function isValidated(string $field): bool
```

Checks 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() <span id="method-setErrors"></span>

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

Replaces all field errors.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$values` | `array<string, array<int, string>>` | Error messages indexed by field name. |