This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
1.0.0 1.0.1
form validation library for Elm
version 1.0.2
license BSD3
native-modules False
elm-version 0.18.0 <= v < 0.19.0
Tag 1.0.2
Committed At 2016-12-03 16:31:54 UTC
elm-lang/html 2.0.0 <= v < 3.0.0 2.0.0
elm-lang/core 5.0.0 <= v < 6.0.0 5.1.1

Modules

README

Elm Form Validation library

Installation

To install the library, use the normal approach with elm-package.

elm-package install billperegoy/elm-form-validations

Model and Init

The form data is represented by a single sub-model instantiated within your user’s model. For instance.

import Forms
type alias model =
    { loginForm : Forms.Form
    }

You can (and will) of course add other data to your model, but in order to acquire and validate form data, you just need this one entry. In order to define what fields are available in your form, you need to define an init function that describes this.

init =
    { loginForm = Form.initForm loginFormFields } ! []

The loginFormFields function is where all the field definition occurs.

loginFormFields : List ( String, List Forms.FieldValidator )
loginFormFields =
    [ ("name", nameValidations
    , ( "email", emailValidations )
    , ( "password", passwordValidations )
    ]

This function returns a list of tuples with each tuple representing the field name and a list of validations for that field. The field validation lists are contained in separate functions. Here is an example of one of them.

passwordValidations : List Forms.FieldValidator
passwordValidations =
    [ Forms.validateExistence
    , Forms.validateMinLength 10
    , Forms.validateMaxLength 15
    ]

This example adds three validations to the password field. Each of the validation functions is a partially applied function defined in the library. Upon initialization, all data structures to support the input and validation of the defined fields is set up. While under the hood, an Elm Dict is used to hold this data, there is no need to know those details. You can just use the supplied functions to update and query this structure.

Update Function

You only need to add one message type and update action to update and access any form field defined in the init function.

type Msg
    = UpdateFormText String String
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        UpdateFormText name val ->
            { model
                | signupForm =
                    Forms.updateFormInput model.signupForm name val
            }
                ! []

That’s really all there is to it. You define this single message type and update case and the library handles the rest. The updateFormContent function does the work of storing the value and running validations. All that’s left now is to define our view so that we can trigger updates and display the form values and validate status.

View Function

view : Model -> Html Msg
view model =
    div []
        [ Html.form []
            [ input
                  [ placeholder "Enter email"
                  , onInput (UpdateFormText "email")
                  ]
                  []
            , small []
                  [ text (Forms.errorString form "email") ]
            ]
        ]

Full Example

For a full working example of this librray in use, please visit.

https://github.com/billperegoy/elm-forms-example