Giter VIP home page Giter VIP logo

elm-form-field's Introduction

Field

A Field is a simple data type that helps capture and validate form data better. The left side of a field represents a function that takes in a value of arbitrary type, validates it and returns a ValidationResult.

The ValidationResult can contain the value of the field if the validation Passed, or an error if the validation Failed

Example

Assume that we have a form with name, email and phone as inputs. Then we can define these as Fields as follows

import Field exposing (..)
import Field.ValidationResult exposing (..)

type alias Model =
    { name : Field String String
    , email : Field String String
    , phone : Field String String
    }

The left String of a Field refers to the error and the right String refers to the actual value type

We can initialize these as follows

init =
    { name = Field validateEmpty ""
    , email = Field (validateEmpty >> andThen validateEmail) ""
    , phone = Field validateNumbersOnly ""
    }

The Field constructor takes two arguments. The first one is a validation function which takes in value of the field and produces a ValidationResult type

validateEmpty : String -> ValidationResult String String
validateEmpty s =
    case s of
        "" ->
            Failed "Field cannot be empty"

        _ ->
            Passed s

Validate a Field using the validate function.

Example of a Passed validation:

name = Field validateEmpty "John Doe"

nameValidationResult = validate name

-- nameValidationResult == Passed "John Doe"

Example of a Failed validation:

email = Field validateEmpty ""

emailValidationResult = validate email

-- emailValidationResult == Failed "Field cannot be empty"

Refer example for more

Documentation

Refer documentation here

About BigBinary

BigBinary

elm-reader is maintained by BigBinary. BigBinary is a software consultancy company. We build web and mobile applications using Ruby on Rails, React.js, React Native and Elm.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.