This module helps with validation of input forms.
Define data type. For example:
dist =
Field 40.5 NotValidated
That means dist
has value 40.5 which hasn't validated yet.
This type defines three state of Field:
NotValidated
values e.g. in input from, which have not validated yet.Valid a
values that have been validated and are correct.Invalid String
and state for incorrect input values.Event describe state of input form:
OnSubmit
validates model data before submitting to server,
see validateModel
in example
.OnBlur
validates input form when user leaves an input field.OnRelatedChange
validates input form which is tied with another form.
For example: password and confirm form.OnChange raw
validates input form when user changes value in input field,Here SubmissionStatus
define states for submit data to server:
NotSubmitted
means that data have not sent yet.
InProcess
for data being processed.
Succeeded
if data have been successfully received.
Failed
or vice versa, data have not been successfully received.
This also may be used to inform user on screen, see renderStatus
in example
.
Field raw (Maybe a)
Sometimes we want to use input form as optional, for example age. In this case the input field can be an empty. But if somebody provides input value then input field will be validated. So this type is used for define optional variable.
a -> Result String b
It's used for validate variables, see Validators
for example.
String
Represents error message for invalid values in input form. This type is used for definition of validator functions.
extractError : Field raw a -> Maybe String
Extract error message from Field.
import Validaton exposing (Field)
errorLabel : Field raw a -> Html Msg
errorLabel field =
div []
[ field
|> extractError
|> Maybe.withDefault ""
|> text
]
field : b -> Field b a
Default setting of Field with NotValidated
validity.
import Validation exposing (Field, field)
intValue : Field String String
intValue =
field "50"
-- Field "50" NotValidated
preValidatedField : (val -> String) -> val -> Field String val
Default setting of Field with Valid a
validity.
For Field String String
use an identity function.
import Validation exposing (Field, field)
intValue : Field String Int
intValue =
preValidatedField String.fromInt 50
-- Field "50" (Valid 50)
validate : Event raw -> Validator raw a -> Field raw a -> Field raw a
Run validation on Field with Event
.
validity : Field raw a -> Validity a
Get validity from Field.
import Validation exposing (Field)
intValue : Field String Int
intValue =
Field "50" (Valid 50)
validity intValue -- Valid 50
rawValue : Field b a -> b
Get value from Field.
import Validation exposing (Field)
intValue : Field String String
intValue =
Field "50" NotValidated
rawValue intValue -- "50"
optional : Validator String a -> String -> Result String (Maybe a)
Validation of optional variable.
import Validaton exposing (Event(..), OptionalField, field, validate)
import Validators exposing (isPositiveInt)
age : OptionalField String Int
age =
field ""
validate
age
|> optional (isPositiveInt "The age has to be positive number.")
|> OnSubmit
-- Field "" (Valid Nothing)
invalidate : String -> Field raw a -> Field raw a
Default setting of Field with Valid a
validity.
For Field String String
use an identity function.
import Validation exposing (Field, invalidate, preValidatedField)
intValue : Field String Int
intValue =
preValidatedField String.fromInt 50
invalidate "Bad value" intValue -- Field "50" (Invalid "Bad value")
applyValidity : Validity a -> Validity (a -> b) -> Validity b
Applying function to particular validation. For instance, see submitIfValid
and
submit
function in example
.
composite : Validator a b -> Validator b c -> Validator a c
Composition of two Validators.
import Validators exposing (isEmail, isNotEmpty)
emailValidation : Validator String String
emailValidation =
composite
(isNotEmpty "An email is required.")
(isEmail "Please ensure this is a valid email.")