# nano-ui-form
Validated applicative forms for [nano-ui](https://github.com/goolord/nano-ui), built on [ditto](https://hackage.haskell.org/package/ditto). Backend-independent.
Each form combines widget rendering, parsing, and validation in one applicative value, showing field-level error messages beneath invalid inputs.
```haskell
{-# LANGUAGE OverloadedStrings #-}
import Data.Text (Text)
import NanoUI (NanoUI, label)
import NanoUI.Form
data Signup = Signup Text Text
signup :: Form Text Signup
signup =
Signup
<$> withFieldErrors (inputText "Name" "" `prove` notEmpty "Name is required")
<*> withFieldErrors (inputText "Email" "" `prove` validEmail (const "Not an email address"))
-- Allocate owner <- newFormState once during setup.
view :: FormState -> NanoUI ()
view owner = do
submitted <- nanoFormSubmit owner "signup" "Sign up" signup
case submitted of
Just (Signup name _) -> label ("Welcome, " <> name)
Nothing -> pure ()
```
## Runners and state
Allocate `newFormState` once per component or session and pass a unique stable key (`"signup"`) to every runner and to `resetForm`.
- `nanoFormSubmit` renders a submit button and returns `Just a` on the single frame the form submits validly (store or handle the result on that frame).
- `nanoFormLive` returns `Just a` on every frame while all fields are valid.
- `nanoFormEx` takes a `FormConfig`, and `runNanoForm` separates rendering from extracting the result (call `markSubmitted` when using a custom submit button).
With `OverloadedStrings`, plain string literals set both a field's key and caption; use `named` and `unnamed` in `NanoUI.Form.Input` to separate keys from labels or use positional identity. Wrap any `Var a -> NanoUI Response` widget as a form field with `inputWidget`.
## Documentation
- [The nano-ui Book: Validated Forms](https://goolord.github.io/nano-ui/book/packages/index.html#validated-forms): Companion package overview and examples.