This module helps to transform validated forms to models.
This function is similar to function above and is used for non-field type of variables.
import Validation exposing (preValidatedField, Field)
type Planet = Venus | Earth | Mars
type alias Model = { planet : Planet, a: Int, b: Float}
type alias Form = { planet : Planet, a : Field String Int, b : Field String Float}
form = Form Earth (preValidatedField 1) (preValidatedField 0.3)
--form = Form Mars (preValidatedField 1) (Validation.field "40.5")
model : Result String Model
model =
let
fieldOk = withField Ok
valueOk = withoutField Ok
in
toModel
Model
( valueOk .planet
>> fieldOk .a
>> fieldOk .b
)
form
-- Ok { planet = Earth, a = 1, b = 0.3 }
-- for form with Mars we get:
-- Err "Form is invalid!!!",
-- because validity of initial value b is NotValidated.This function transforms form to model and returns an Err errorMessage if
any variable in the form is NotValidated either Invalid. Otherwise returns Ok model.
See examples above.
This function is used to check if the given variable in the form has correct validity and if yes, then returns this variable with her value. Otherwise returns an
Err errorMessage.