Skip to content

Instantly share code, notes, and snippets.

Created April 8, 2017 08:19
Show Gist options
  • Select an option

  • Save anonymous/2fd6ccee4b3783d342d7d872533eff55 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/2fd6ccee4b3783d342d7d872533eff55 to your computer and use it in GitHub Desktop.
testing elm-form part 2
{
"version": "1.0.0",
"summary": "Validate form fields with friendly error messages",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "5.1.1 <= v < 5.1.1",
"elm-lang/html": "2.0.0 <= v < 2.0.0",
"etaque/elm-form": "2.0.0 <= v < 2.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}
<html>
<head>
<style>
html {
background: #F7F7F7;
color: red;
}
</style>
</head>
<body>
<script>
var app = Elm.Main.fullscreen()
</script>
</body>
</html>
module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Form exposing (Form)
import Form.Validate as Validate exposing (..)
import Form.Input as Input
type alias User =
{ name : String
, password : String
, passwordConfirmation : String
}
type alias Model =
{ form : Form String User }
type Msg
= NoOp
| FormMsg Form.Msg
init : ( Model, Cmd Msg )
init =
( { form = Form.initial [] validate }, Cmd.none )
validate : Validation String User
validate =
succeed User
|> andMap (field "name" (string |> withCustomError "Input Name !"))
|> andMap (field "password" (string |> withCustomError "Input Password !"))
|> andMap
(oneOf
[ (field "password" string) |> andThen validateConfirmation
, emptyString |> andThen validateConfirmation
]
)
validateConfirmation : String -> Validation String String
validateConfirmation password =
field "passwordConfirmation"
(string
|> withCustomError "Input Password Confimation !"
|> andThen
(\confirmation ->
if password == confirmation then
succeed confirmation
else
fail (customError "Confirmation Error")
)
)
-- Forward form msgs to Form.update
update : Msg -> Model -> ( Model, Cmd Msg )
update msg ({ form } as model) =
case msg of
NoOp ->
( model, Cmd.none )
FormMsg formMsg ->
( { model | form = Form.update validate formMsg form }, Cmd.none )
-- Render form with Input helpers
view : Model -> Html Msg
view { form } =
Html.map FormMsg (formView form)
formView : Form String User -> Html Form.Msg
formView form =
let
-- error presenter
errorFor field =
case field.liveError of
Just error ->
-- replace toString with your own translations
div [ class "error" ] [ text (toString error) ]
Nothing ->
text ""
-- fields states
name =
Form.getFieldAsString "name" form
password =
Form.getFieldAsString "password" form
passwordConfirmation =
Form.getFieldAsString "passwordConfirmation" form
in
div []
[ div []
[ label [] [ text "Name" ]
, Input.textInput name []
, errorFor name
]
, div []
[ label [] [ text "Password" ]
, Input.textInput password []
, errorFor password
]
, div []
[ label [] [ text "Password Confirmation" ]
, Input.textInput passwordConfirmation []
, errorFor passwordConfirmation
]
, button
[ onClick Form.Submit ]
[ text "Submit" ]
]
main =
Html.program
{ init = init
, update = update
, view = view
, subscriptions = \_ -> Sub.none
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment