---
title: "framework — Form Lifecycle & Validation Reference"
---

> Documentation Index
> Fetch the complete documentation index at: https://docs.middag.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# framework — Form Lifecycle & Validation Reference

# Form Lifecycle & Validation Reference

Reference for how a `Form` moves from construction to submission, how field-level
validation is enforced, and how the `FormRequest` escalation path relates to it.
Backs [FW-003](https://github.com/middag-io/middag-php-framework/blob/main/docs/decisions/FW-003-schema-first-forms-form-request-escalation.md).

## Lifecycle

| Stage | Trigger | What happens |
|---|---|---|
| Construct | `$container->get(SiteForm::class)` (autowired) | `AbstractForm::__construct()` injects `FormValidator`; the form starts unsubmitted. |
| Hydrate | `FormResolver`, only on `POST`/`PUT`/`PATCH`/`DELETE` | `hydrate(array $input)` stores the submitted values on the form's internal `FormState`. On `GET` (and other safe methods) the form is left unhydrated — initial render state. |
| Validate | Same resolver call, immediately after hydrate | `validate()` walks `schema()` against the submitted values via `FormValidator`; errors are attached to the form state. |
| Submit | Controller reads `isSubmittedAndValid()` | `true` only when the form was hydrated (a mutating verb ran) **and** `errors() === []`. `validated()` returns the submitted values. |

`FormResolver` (`Http/Resolver/FormResolver.php`, `@internal`) runs hydrate + validate
automatically whenever a controller method type-hints a `FormInterface` subclass — the
same resolver-chain mechanism the HTTP kernel uses for routing/auth.

## Declaring a schema

```php
final class SiteForm extends AbstractForm
{
public function schema(): array
{
    return [
        FieldFactory::text('name')->label('site.name', 'ecommerce')->required()->max(255),
        FieldFactory::url('url')->label('site.url', 'ecommerce')->required(),
        FieldFactory::select('provider')->label('site.provider', 'ecommerce')
            ->options(['eduzz' => 'provider.eduzz', 'woocommerce' => 'provider.woocommerce'])
            ->required(),
        FieldFactory::password('api_key')->label('site.api_key', 'ecommerce')
            ->visibleWhen('provider', ConditionOperator::In, ['eduzz'])
            ->requiredWhen('provider', ConditionOperator::In, ['eduzz']),
    ];
}
}
```

Field names must match `/^[a-z][a-z0-9_]*$/` (snake_case) and cannot be one of the
reserved names `id`, `submit`, `cancel`, `save`, `_token` — enforced at construction, not
left to convention.

### Field factory

`FieldFactory` is the only supported way to create a field (`@api`); concrete field
classes are `@internal`.

| Method | Field | Notes |
|---|---|---|
| `text` / `textarea` | Text | — |
| `email` / `password` / `url` | Text variant | Same `TextField` class, distinguished by an explicit `FieldType` — no separate class per variant. |
| `integer` / `decimal` | Numeric | `max`/`min` bound the numeric value itself, not a string length. |
| `checkbox` / `toggle` | Boolean | — |
| `date` / `datetime` | Date | — |
| `duration` | Duration | — |
| `select` / `multiselect` / `radio` | Choice | `options()` (static array) or `optionsFrom()` (lazy loader). |
| `entityPicker` | Entity reference | — |
| `file` | Upload | — |
| `header` / `display` | Static | No submitted value. |
| `hidden` | Hidden | — |

### Common fluent methods (every field)

| Method | Effect |
|---|---|
| `label(key, component)` / `help(key, component)` | i18n intent, resolved through the translation contract — never a raw string. |
| `default(value)` | Seeds the rendered control and the client props. |
| `required()` | Statically required — enforced server-side and exposed to the renderer. |
| `readonly()` | Read-only, both internally and as a renderer prop. |
| `visibleWhen` / `hiddenWhen` / `disabledWhen` | Conditional UI behaviour — **client-only**, not enforced by `FormValidator`. |
| `requiredWhen` | Conditional requirement — the one condition kind `FormValidator` also enforces server-side. |
| `rule(Constraint)` | Attaches a raw Symfony `Constraint` (e.g. `new Assert\Email()`) — enforced server-side, never sent to the client. |
| `meta(key, value)` | Arbitrary metadata; the Inertia/React renderer does not forward it. |

Conditional behaviour is schema data (a `Condition` value object), not a procedural
callback — the same declaration renders correctly on every renderer target.

## Server-side validation

`FormValidator` (`Form/FormValidator.php`, `@internal`) walks the schema and validates
submitted values using the **Symfony Validator** (`symfony/validator`).

| Field declaration | Symfony constraint applied |
|---|---|
| `required()` / `requiredWhen()` evaluates true | `Assert\NotBlank` |
| `max(n)` | `Assert\LessThanOrEqual(n)` for numeric fields, `Assert\Length(max: n)` otherwise |
| `min(n)` | `Assert\GreaterThanOrEqual(n)` for numeric fields, `Assert\Length(min: n)` otherwise |
| `pattern(...)` | `Assert\Regex` (non-numeric fields only) |
| `rule(Constraint)` | The given constraint, verbatim |

An optional field left empty (`null` or `''`) is skipped entirely — mirrors Symfony's own
null/empty handling and keeps comparison constraints from firing on nothing. Since
framework 1.24.0, violations come back per field as ui
[`FieldMessage`](/framework/reference/ui/wire-contract/#form-errors) objects (`{message, key, domain, params?}`):
one when a single constraint fails, or a list when several fail on the same field.
`message` is the violation message, already translated when a translator is configured;
`key`, `domain` and `params` let a client render it in its own language. Messages are no
longer flattened to strings.

## `FormRequest` — standalone or companion validation

`AbstractFormRequest` (`Http/Request/AbstractFormRequest.php`) is a second, independent
validation path — not a subtype of `Form`. A subclass declares:

```php
use Symfony\Component\Validator\Constraints as Assert;

final class SiteStoreRequest extends AbstractFormRequest
{
public function rules(): array
{
    return [
        'title' => [new Assert\NotBlank(), new Assert\Length(max: 255)],
        'email' => new Assert\Email(),
        'age'   => new Assert\Optional([new Assert\Type('integer'), new Assert\Positive()]),
    ];
}
}
```

`FormRequestResolver` resolves and validates it automatically whenever a controller
method type-hints a `FormRequestInterface` subclass. Input is validated as an
`Assert\Collection`: every declared field must be present unless wrapped in
`Assert\Optional`; undeclared input keys are ignored. Failure throws
`MiddagValidationException` (HTTP 422) carrying a `field => message(s)` map; success
exposes only the declared fields via `validated()`.

### When to use which

| Scenario | Mechanism |
|---|---|
| Field-level rules, simple conditional requirements | `schema()` on the `Form` alone. |
| Cross-field or DB-backed validation on top of a form | Both — the controller method type-hints the `Form` **and** a `FormRequest` as two separate parameters; each resolver runs independently. |
| A pure REST endpoint with no UI | `FormRequest` alone, no `Form`. |
| Validation shared between a form and a REST endpoint over the same payload | The same `FormRequest` class, type-hinted by both controller methods. |

Pairing a `Form` with a `FormRequest` today is a controller-signature convention (type-hint
both), not a binding the framework reads off a class constant — `AbstractForm` and
`FormResolver` carry no such property. When both declare a rule for the same field, the
`FormRequest` rule is the one that applies — never duplicate a rule across the two.

## `#[ValidatedDto]` — a typed DTO as the single declaration

The third input path marks a controller parameter with [`#[ValidatedDto]`](https://github.com/middag-io/middag-php-framework/blob/main/src/Http/Attribute/ValidatedDto.php) (`@api`). The parameter's type is a plain class with no framework base class whose properties carry Symfony `#[Assert\*]` constraints:

```php
public function store(#[ValidatedDto] TicketRequest $ticket): Response
{
// $ticket is hydrated and validated here.
}
```

`ValidatedDtoResolver` (`@internal`) reads the request input (query, body and JSON), hydrates the object (snake_case input keys map to camelCase properties, scalars are coerced) and validates it before the action runs. Optional inputs should be nullable properties with a default.

The same properties can also carry [`#[Field]`](https://github.com/middag-io/middag-php-framework/blob/main/src/Form/Attribute/Field.php) (`@api`), which `FieldSchemaReader` reflects into the same field list `schema()` returns. One DTO can then be the single declaration for form shape and validation. `#[Field]` covers declarative scalar fields; options, conditions and entity pickers still use the fluent `schema()` path.

## One error shape

`FormRequest` and `#[ValidatedDto]` both throw `MiddagValidationException` (HTTP 422) carrying a `field => message(s)` map. The kernel answers it in one of two ways:

| Request | Response |
| --- | --- |
| JSON/XHR API, or no `FlashBag` bound | `422` with `{success: false, error: 'validation_failed', message, errors}` |
| Browser or Inertia visit with a `FlashBag` bound | Errors flashed to the session and a `303` back to the referrer; `ShareFlashMiddleware` re-shares them as the Inertia `errors` prop |

Since framework 1.24.0, each field error in both responses is a ui `FieldError`: a `FieldMessage` (`{message, key, domain, params?}`, `params` omitted when empty) or a list of them. An empty error map is flashed and shared as `{}`.

When the request carries an `X-Inertia-Error-Bag` header, the field map is nested under that bag name. In the 422 body, `errors` becomes `{bag: {field: ...}}`. For the flash path, `FlashBag::setErrors(FormErrors $errors, ?string $bag = null)` is the single writer and `FlashBag::pullErrors()` the single reader: it returns a ui `FormErrorBags` when a bag was named and a `FormErrors` otherwise, which `ShareFlashMiddleware` shares as the `errors` prop (see [Error bags](/framework/reference/ui/wire-contract/#error-bags)).

## i18n

Labels, help text, option lists and validation error messages never carry a raw string.
`label()` / `help()` resolve to a `Translatable` (translation key + host domain);
`FormValidator` optionally routes Symfony violation messages through the host's
`TranslatorInterface` (translation domain `validators`) via `SymfonyTranslatorAdapter` —
without a configured translator, Symfony's default English messages pass through
unchanged.

Source: https://docs.middag.dev/framework/reference/form-lifecycle-and-validation/index.mdx
