cekrem/elm-form/1.0.0

NOTE: the latest version is 1.1.1

Form

Build type-safe, declarative forms with validation and transformation support.

This module provides a simple way to create forms by composing inputs together. It handles form state management, validation, and transformation of user input.

Types

type Form msg

An opaque type representing a form with multiple inputs.

A Form contains form-level attributes and a collection of inputs that will be rendered together.

type alias FormInput msg =
{ key : String
, input_ : Input Dumb msg
}

Represents an input field within a form.

Each input has a unique key that identifies it in the form's value dictionary, and configuration for how the input should behave and appear.

Note: You should create FormInput values using the input function rather than constructing this record directly. The input function provides a clean, type-safe way to create form inputs.

Creating Forms

new : List (Attribute msg) -> List (FormInput msg) -> Form msg

Create a new form with form-level attributes and inputs.

import Html.Attributes as Attr


myForm : Form Msg
myForm =
    new [ Attr.class "my-form" ]
        [ input "email" "Email Address"
            |> withType "email"
            |> withRequired True
        , input "name" "Full Name"
            |> withRequired True
        ]
input : String -> String -> FormInput msg

Create a new form input with a key and label.

The key is used to identify the input's value in the form state dictionary.

input "username" "Username"


input "bio" "Biography"
    |> withType "textarea"

Configuring Inputs

withRequired : Bool -> FormInput msg -> FormInput msg

Mark an input as required or optional.

input "email" "Email"
    |> withRequired True
withType : String -> FormInput msg -> FormInput msg

Set the input type (e.g., "text", "email", "password", "number").

input "password" "Password"
    |> withType "password"
    |> withRequired True


input "age" "Age"
    |> withType "number"
withPlaceholder : String -> FormInput msg -> FormInput msg

Add placeholder text to an input.

input "search" "Search"
    |> withPlaceholder "Type to search..."


input "email" "Email Address"
    |> withType "email"
    |> withPlaceholder "[email protected]"
withTransformer : (String -> String) -> FormInput msg -> FormInput msg

Add a transformer function that processes the input value before it's stored.

Transformers are useful for formatting input, such as removing whitespace or converting to a specific case.

input "username" "Username"
    |> withTransformer String.trim


input "code" "Postal Code"
    |> withTransformer String.toUpper
(String -> Result (List (Attribute msg)) ())
-> FormInput msg
-> FormInput msg

Add a validator function that returns additional HTML attributes on validation failure.

Validators can return attributes like CSS classes or ARIA attributes to provide visual feedback when validation fails.

import Html.Attributes as Attr


emailValidator : String -> Result (List (Html.Attribute msg)) ()
emailValidator value =
    if String.contains "@" value then
        Ok ()
    else
        Err [ Attr.class "error", Attr.attribute "aria-invalid" "true" ]


input "email" "Email"
    |> withType "email"
    |> withValidator emailValidator
withAttributes : List (Attribute msg) -> FormInput msg -> FormInput msg

Add custom HTML attributes to an input.

import Html.Attributes as Attr


input "name" "Name"
    |> withAttributes
        [ Attr.class "form-control"
        , Attr.placeholder "Enter your name"
        ]

Rendering

Dict String String
-> (Dict String String -> msg)
-> msg
-> Form msg
-> Html msg

Render the form to HTML.

Takes the current form values as a dictionary, a message constructor for handling value changes, a message for form submission, and the form configuration.

import Dict exposing (Dict)


type Msg
    = FormChanged (Dict String String)
    | FormSubmitted


view : Dict String String -> Html Msg
view formValues =
    myForm
        |> build formValues FormChanged FormSubmitted