From 68cfe7019fd8e3a8cc962239237b06edc174723e Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Sun, 12 Jun 2022 08:34:04 -0700 Subject: [PATCH] Add checkbox and int fields. --- src/Pages/Field.elm | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/Pages/Field.elm b/src/Pages/Field.elm index 0e20179e..a0402233 100644 --- a/src/Pages/Field.elm +++ b/src/Pages/Field.elm @@ -87,6 +87,66 @@ text = } +{-| -} +checkbox : + Field + error + Bool + { required : () + } +checkbox = + Field + { type_ = "checkbox" + , required = False + , serverValidation = \_ -> DataSource.succeed [] + , decode = + \rawString -> + ( (rawString == Just "on") + |> Just + , [] + ) + , properties = [] + } + + +{-| -} +int : + { invalid : String -> error } + -> + Field + error + (Maybe Int) + { min : Int + , max : Int + , required : () + , wasMapped : No + , initial : Int + } +int toError = + Field + { type_ = "number" + , required = False + , serverValidation = \_ -> DataSource.succeed [] + , decode = + \rawString -> + case rawString of + Nothing -> + ( Nothing, [] ) + + Just "" -> + ( Nothing, [] ) + + Just string -> + case string |> String.toInt of + Just parsedInt -> + ( Just (Just parsedInt), [] ) + + Nothing -> + ( Nothing, [ toError.invalid string ] ) + , properties = [] + } + + {-| -} withClientValidation : (parsed -> ( Maybe mapped, List error )) -> Field error parsed constraints -> Field error mapped { constraints | wasMapped : Yes } withClientValidation mapFn (Field field) =