# form Type-safe decoding and validation of HTML form data in pure Louise: a port of the Gleam package [`formal`](https://hex.pm/packages/formal) (Apache-2.0). ```louise import form pub type SignUp { SignUp(email: String, password: String) } /// A schema that decodes a `SignUp` from form data. fn signup_schema :: -> form.Schema { use email <- form.field "email" (form.parse_email ()) use password <- form.field "password" ({ form.parse_string () |> form.check_string_length_more_than 7 }) use _ <- form.field "confirm" ({ form.parse_string () |> form.check_confirms password }) form.success SignUp(email: email, password: password) } ``` ## In a route `http.form_body` gives the submitted fields as the list of string pairs `add_values` takes. `run` answers the decoded value, or the form with the errors attached — which keeps the user's input, for re-rendering: ```louise fn register :: req: Request -> Response { match form.new (signup_schema ()) |> form.add_values (http.form_body req) |> form.run { Ok(signup) -> create_account signup , Error(f) -> http.html (signup_page f) // form.field_error_messages(f, "email") } } ``` ## Parsers and checks Parsers (`parse_*`) extract a value from a field's input; checks (`check_*`) constrain it. Parsers *short-circuit*: when one fails, the checks chained after it don't run, so `parse_int() |> check_int_more_than(0)` on `"abc"` reports `[MustBeInt]` and nothing more. Checks don't short-circuit each other: every failed check adds its error. | Parser | Answers | Fails with | |---|---|---| | `parse_string()` | `String` | never | | `parse_int()` | `Int` | `MustBeInt` | | `parse_float()` | `Float` (an int reads fine) | `MustBeFloat` | | `parse_email()` | `String` containing `@` | `MustBeEmail` | | `parse_phone_number()` | digits, `+ - ( )` removed | `MustBePhoneNumber` | | `parse_checkbox()` | `Bool`: any value is `True`, absent `False` | never | | `parse_colour()` | `#` + six hex digits | `MustBeColour` | | `parse_list(p)` | `p` per value of a repeated field | | | `parse_optional(p)` | `Some(p)` when the field has text, else `None` | | | `parse(f)`, `map(p, f)` | your own | `CustomError` | Checks: `check_not_empty`, `check_accepted`, `check_confirms` (password confirmation), `check_int/float_more_than/less_than`, `check_string_length_more_than/less_than`, and `check` for your own. ## Errors A failed `run` attaches `FieldError`s to their fields; read them as values (`field_errors`) or as text (`field_error_messages`), and add your own from outside the schema with `add_error` (say, an email that's already registered). The text comes from a translator — `en_gb` by default, `en_us` for the other spelling of "colour", or your own with `language`: ```louise form.new schema |> form.language my_translator ``` ## Variants A form whose shape depends on a hidden input parses the marker first, then branches — each arm is its own schema: ```louise use kind <- form.field("kind", form.parse_string()) match kind { "guest" -> { use email <- form.field("email", form.parse_email()) form.success(Guest(email)) } , _ -> ... } ``` ## Tests `louise test` here, and `louise test --native` for the native backend.