This library helps with validation of input forms.
This type defines three state of Field:
NotValidated values e.g. in input from, which have not validated yet.Valid a values that they 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 renderStatusin example.
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.
It's used for validate variables, see Validators for example.
Represents error message for invalid values in input form. This type is used for definition of validator functions.
Extract error message from Field.
import Validaton exposing (Field)
errorLabel : Field raw a -> Html Msg
errorLabel field =
div []
[ field
|> extractError
|> Maybe.withDefault ""
|> text
]Default setting of Field with NotValidated validity.
import Validation exposing (Field, field)
intValue : Field String String
intValue =
field "50"
-- Field "50" NotValidatedDefault 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)Run validation on Field with Event.
Get validity from Field.
import Validation exposing (Field)
intValue : Field String Int
intValue =
Field "50" (Valid 50)
validity intValue -- Valid 50Get value from Field.
import Validation exposing (Field)
intValue : Field String String
intValue =
Field "50" NotValidated
rawValue intValue -- "50"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)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")Applying function to particular validation. For instance, see submitIfValid and
submit function in example.
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.")
Define data type. For example:
That means
disthas value 40.5 which hasn't validated yet.