# Module /web/app/forms

HTML form binding, type coercion, CSRF token fields (<csrf-field>), and field validation errors (<form-error>).

```tangu
(use /web/app/forms)
```

## Functions & symbols

### Validation

`(Validation ok errors) -> Validation`

Re-exported Validation result struct from `/validation`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `ok` | `Bool` | no | Overall validation success flag |
| `errors` | `Map` | no | Map of {:field [error-messages...]} |

```tangu
(let res (Validation true {}))
```

### ValidationErrors

`(ValidationErrors errors [values]) -> ValidationErrors`

Re-exported ValidationErrors struct from `/validation`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `errors` | `Map` | no | Field error arrays keyed by field KeySymbol |
| `values` | `Map` | yes | Submitted raw input values |

```tangu
(ValidationErrors {:email ["Invalid email"]})
```

### add-field-error

`(add-field-error target field message)`

Attaches a custom error message to `field` on `target` (a Struct, `ValidationErrors`, or `Map`), returning a `ValidationErrors`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `target` | `Any` | no | — |
| `field` | `Any` | no | — |
| `message` | `Any` | no | — |

### coerce-field-value

`(coerce-param-value raw-value type-name constraints)`

Coerces a raw form field value into `type-name` using the unified parameter coercion engine.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `raw-value` | `Any` | no | — |
| `type-name` | `Any` | no | — |
| `constraints` | `Any` | no | — |

### extract-inner-type

`(extract-inner-type type-str)`

Extracts the inner type name from an optional `(Maybe T)` or `(Or T)` type string.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `type-str` | `Any` | no | — |

### first-error-of

`(first-error-of target)`

Returns the first validation error message across all fields in `target`, or `()` if none exist.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `target` | `Any` | no | — |

### flat-errors-of

`(flat-errors-of target)`

Projects `target`'s errors into a flat `{:field "first error"}` map suitable for UI components.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `target` | `Any` | no | — |

### get-field-error

`(get-field-error target field)`

Returns the first error message string for `field` on `target`, or `()` if valid.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `target` | `Any` | no | — |
| `field` | `Any` | no | — |

### get-field-errors

`(get-field-errors target field)`

Returns the array of error messages for `field` on `target`, or `[]` if valid.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `target` | `Any` | no | — |
| `field` | `Any` | no | — |

### get-field-value

`(get-field-value ...args)`

Looks up a field value on a Struct instance, `ValidationErrors`, or `Map`, returning `default-val` when absent.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `args` | `Any` | yes | — |

### has-field-error?

`(has-field-error? ...args)`

Returns true if `target` has any validation error (or an error on `field` when `field` is provided).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `args` | `Any` | yes | — |

### is-file-selected?

`(is-file-selected? upload)`

Returns true if a multipart file upload map contains a non-empty filename and non-empty content.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `upload` | `Any` | no | — |

### is-maybe-type?

`(is-maybe-type? type-str)`

Returns true if `type-str` is an optional `(Maybe ...)` or `(Or ...)` type.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `type-str` | `Any` | no | — |

### is-valid?

`(is-valid? instance [schema]) -> Bool`

Re-exported validation check predicate from `/validation`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `instance` | `Any` | no | Struct instance or Map |
| `schema` | `Struct | Map` | yes | Optional target Struct descriptor |

```tangu
(assert! (validation/is-valid? user))
```

### merge-form-errors

`(merge-form-errors ...targets)`

Merges two or more `ValidationErrors` (or ` ():` / valid targets) into a single `ValidationErrors`, or returns `()` if all are valid.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `targets` | `Any` | yes | — |

### read-form

`(read-form req struct-type)`

Reads and coerces a request's form body into `struct-type` (re-exported from `/web/app`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `req` | `Any` | no | — |
| `struct-type` | `Any` | no | — |

### respond-form-errors

`(respond-form-errors ...args)`

Returns a 422 Unprocessable Entity response negotiated for JSON, Stage/HTML (via `render-fn`), or plain text.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `args` | `Any` | yes | — |

### validate

`(validate instance [schema]) -> Validation`

Re-exported struct validation function from `/validation`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `instance` | `Any` | no | Struct instance or Map to validate |
| `schema` | `Struct | Map` | yes | Optional target Struct descriptor when validating raw Maps |

```tangu
(let v (validation/validate signup-form))
```

### validate-form

`(validate-form ...args)`

Validates a form struct instance or parses and validates `req` against `struct-type`, returning a `Validation`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `args` | `Any` | yes | — |

### validate-upload

`(validate-upload upload ...rest)`

Validates a multipart file upload map against `:required`, `:max-bytes`, and `:accept` rules, returning `()` or `ValidationErrors`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `upload` | `Any` | no | — |
| `rest` | `Any` | yes | — |
