Include info to render field view in Validation type.

This commit is contained in:
Dillon Kearns 2022-07-20 20:56:26 +02:00
parent 7df69aa6d0
commit cdfca81338
4 changed files with 74 additions and 46 deletions

View File

@ -346,7 +346,7 @@ field name (Field fieldParser kind) (Form definitions parseFn toInitialValues) =
parsedField : ParsedField error parsed
parsedField =
Pages.Internal.Form.Validation (Just name) ( maybeParsed, Dict.empty )
Pages.Internal.Form.Validation Nothing (Just name) ( maybeParsed, Dict.empty )
rawField : ViewField error parsed kind
rawField =
@ -419,7 +419,7 @@ field name (Field fieldParser kind) (Form definitions parseFn toInitialValues) =
field2 :
String
-> Field error parsed data kind constraints
-> FormNew error (Validation error parsed named -> parsedAndView) data
-> FormNew error (Validation error parsed kind -> parsedAndView) data
-> FormNew error parsedAndView data
field2 name (Field fieldParser kind) (FormNew definitions parseFn toInitialValues) =
FormNew
@ -439,9 +439,16 @@ field2 name (Field fieldParser kind) (FormNew definitions parseFn toInitialValue
Nothing ->
( Maybe.map2 (|>) maybeData fieldParser.initialValue, Form.NotVisited )
parsedField : ParsedField error parsed
thing : Pages.Internal.Form.ViewField kind
thing =
{ value = rawFieldValue
, status = fieldStatus
, kind = ( kind, fieldParser.properties )
}
parsedField : Validation error parsed kind
parsedField =
Pages.Internal.Form.Validation (Just name) ( maybeParsed, Dict.empty )
Pages.Internal.Form.Validation (Just thing) (Just name) ( maybeParsed, Dict.empty )
rawField : ViewField error parsed kind
rawField =
@ -455,7 +462,7 @@ field2 name (Field fieldParser kind) (FormNew definitions parseFn toInitialValue
myFn :
{ result : Dict String (List error)
, parsedAndView : Validation error parsed named -> parsedAndView
, parsedAndView : Validation error parsed kind -> parsedAndView
, serverValidations : DataSource (List ( String, List error ))
}
->
@ -470,9 +477,9 @@ field2 name (Field fieldParser kind) (FormNew definitions parseFn toInitialValue
fieldParser.serverValidation rawFieldValue
|> DataSource.map (Tuple.pair name)
validationField : Validation error parsed named
validationField : Validation error parsed kind
validationField =
Pages.Internal.Form.Validation (Just name) ( maybeParsed, Dict.empty )
parsedField
in
{ result =
soFar.result
@ -527,7 +534,7 @@ hiddenField name (Field fieldParser _) (Form definitions parseFn toInitialValues
parsedField : ParsedField error parsed
parsedField =
Pages.Internal.Form.Validation (Just name) ( maybeParsed, Dict.empty )
Pages.Internal.Form.Validation Nothing (Just name) ( maybeParsed, Dict.empty )
serverValidationsForField : DataSource (List ( String, List error ))
serverValidationsForField =
@ -674,7 +681,7 @@ errorsForField viewField (Errors errorsDict) =
{-| -}
errorsForField2 : Validation error parsed { field : kind } -> Errors error -> List error
errorsForField2 : Validation error parsed kind -> Errors error -> List error
errorsForField2 field_ (Errors errorsDict) =
errorsDict
|> Dict.get (Validation.fieldName field_)
@ -707,8 +714,9 @@ mergeResults :
-> Validation error parsed unnamed
mergeResults parsed =
case parsed.result of
( Pages.Internal.Form.Validation name ( parsedThing, combineErrors ), individualFieldErrors ) ->
Pages.Internal.Form.Validation name
( Pages.Internal.Form.Validation viewField name ( parsedThing, combineErrors ), individualFieldErrors ) ->
Pages.Internal.Form.Validation Nothing
name
( parsedThing
, mergeErrors combineErrors individualFieldErrors
)
@ -722,7 +730,7 @@ mergeResultsDataSource :
-> ( Maybe parsed, DataSource (FieldErrors error) )
mergeResultsDataSource parsed =
case parsed.result of
( Pages.Internal.Form.Validation name ( parsedThing, combineErrors ), individualFieldErrors ) ->
( Pages.Internal.Form.Validation viewField name ( parsedThing, combineErrors ), individualFieldErrors ) ->
( parsedThing
, parsed.serverValidations
|> DataSource.map
@ -911,7 +919,7 @@ runServerSide2 rawFormData (Form _ parser _) =
unwrapValidation : Validation error parsed named -> ( Maybe parsed, FieldErrors error )
unwrapValidation (Pages.Internal.Form.Validation name ( maybeParsed, errors )) =
unwrapValidation (Pages.Internal.Form.Validation viewField name ( maybeParsed, errors )) =
( maybeParsed, errors )
@ -1380,7 +1388,7 @@ helperValues toHiddenInput maybe options formState data (Form fieldDefinitions p
isValid : Bool
isValid =
case merged of
Validation _ ( Just _, errors ) ->
Validation _ _ ( Just _, errors ) ->
Dict.isEmpty errors
_ ->

View File

@ -100,9 +100,9 @@ type Options a
{-| -}
input2 :
List (Html.Attribute msg)
-> Validation error parsed { field : Input }
-> Validation error parsed Input
-> Html msg
input2 attrs (Validation fieldName ( maybeParsed, fieldErrors )) =
input2 attrs (Validation viewField fieldName ( maybeParsed, fieldErrors )) =
-- TODO include `{ value : Maybe String, , kind : ( Input, List ( String, Encode.Value ) ) }` in Validation
let
rawFieldKind =
@ -114,10 +114,18 @@ input2 attrs (Validation fieldName ( maybeParsed, fieldErrors )) =
]
rawField =
{ name = fieldName |> Maybe.withDefault ""
, value = Just ""
, kind = ( rawFieldKind, rawFieldProperties )
}
case viewField of
Just justViewField ->
{ name = fieldName |> Maybe.withDefault ""
, value = justViewField.value
, kind = justViewField.kind
}
Nothing ->
{ name = fieldName |> Maybe.withDefault ""
, value = Just "Nothing case, this shouldn't happen"
, kind = ( rawFieldKind, rawFieldProperties )
}
in
case rawField.kind of
( Input Textarea, properties ) ->

View File

@ -21,8 +21,8 @@ type alias Validation error parsed named =
Pages.Internal.Form.Validation error parsed named
fieldName : Validation error parsed { field : kind } -> String
fieldName (Validation name ( maybeParsed, errors )) =
fieldName : Validation error parsed kind -> String
fieldName (Validation viewField name ( maybeParsed, errors )) =
name
|> Maybe.withDefault ""
@ -30,13 +30,13 @@ fieldName (Validation name ( maybeParsed, errors )) =
{-| -}
succeed : parsed -> Validation error parsed Never
succeed parsed =
Validation Nothing ( Just parsed, Dict.empty )
Validation Nothing Nothing ( Just parsed, Dict.empty )
{-| -}
withFallback : parsed -> Validation error parsed named -> Validation error parsed named
withFallback parsed (Validation name ( maybeParsed, errors )) =
Validation
withFallback parsed (Validation viewField name ( maybeParsed, errors )) =
Validation viewField
name
( maybeParsed
|> Maybe.withDefault parsed
@ -47,32 +47,33 @@ withFallback parsed (Validation name ( maybeParsed, errors )) =
{-| -}
value : Validation error parsed named -> Maybe parsed
value (Validation _ ( maybeParsed, _ )) =
value (Validation _ _ ( maybeParsed, _ )) =
maybeParsed
{-| -}
parseWithError : parsed -> ( String, error ) -> Validation error parsed Never
parseWithError parsed ( key, error ) =
Validation Nothing ( Just parsed, Dict.singleton key [ error ] )
Validation Nothing Nothing ( Just parsed, Dict.singleton key [ error ] )
{-| -}
fail : Validation error parsed1 Named -> error -> Validation error parsed Never
fail (Validation key _) parsed =
Validation Nothing ( Nothing, Dict.singleton (key |> Maybe.withDefault "") [ parsed ] )
fail (Validation _ key _) parsed =
Validation Nothing Nothing ( Nothing, Dict.singleton (key |> Maybe.withDefault "") [ parsed ] )
{-| -}
withError : Validation error parsed1 Named -> error -> Validation error parsed2 named -> Validation error parsed2 named
withError (Validation key _) error (Validation name ( maybeParsedA, errorsA )) =
Validation name ( maybeParsedA, errorsA |> insertIfNonempty (key |> Maybe.withDefault "") [ error ] )
withError (Validation _ key _) error (Validation viewField name ( maybeParsedA, errorsA )) =
Validation viewField name ( maybeParsedA, errorsA |> insertIfNonempty (key |> Maybe.withDefault "") [ error ] )
{-| -}
withErrorIf : Bool -> Validation error ignored Named -> error -> Validation error parsed named -> Validation error parsed named
withErrorIf includeError (Validation key _) error (Validation name ( maybeParsedA, errorsA )) =
Validation name
withErrorIf includeError (Validation _ key _) error (Validation viewField name ( maybeParsedA, errorsA )) =
Validation viewField
name
( maybeParsedA
, if includeError then
errorsA |> insertIfNonempty (key |> Maybe.withDefault "") [ error ]
@ -88,8 +89,8 @@ withErrorIf includeError (Validation key _) error (Validation name ( maybeParsed
{-| -}
map : (parsed -> mapped) -> Validation error parsed named -> Validation error mapped Never
map mapFn (Validation name ( maybeParsedA, errorsA )) =
Validation name ( Maybe.map mapFn maybeParsedA, errorsA )
map mapFn (Validation viewField name ( maybeParsedA, errorsA )) =
Validation Nothing name ( Maybe.map mapFn maybeParsedA, errorsA )
{-| -}
@ -97,10 +98,10 @@ fromResult : Result ( String, error ) parsed -> Validation error parsed Never
fromResult result =
case result of
Ok parsed ->
Validation Nothing ( Just parsed, Dict.empty )
Validation Nothing Nothing ( Just parsed, Dict.empty )
Err ( key, error ) ->
Validation Nothing ( Nothing, Dict.singleton key [ error ] )
Validation Nothing Nothing ( Nothing, Dict.singleton key [ error ] )
{-| -}
@ -111,22 +112,23 @@ andMap =
{-| -}
andThen : (parsed -> Validation error mapped named1) -> Validation error parsed named2 -> Validation error mapped Never
andThen andThenFn (Validation name ( maybeParsed, errors )) =
andThen andThenFn (Validation _ name ( maybeParsed, errors )) =
case maybeParsed of
Just parsed ->
andThenFn parsed
|> (\(Validation _ ( andThenParsed, andThenErrors )) ->
Validation Nothing ( andThenParsed, mergeErrors errors andThenErrors )
|> (\(Validation _ _ ( andThenParsed, andThenErrors )) ->
Validation Nothing Nothing ( andThenParsed, mergeErrors errors andThenErrors )
)
Nothing ->
Validation Nothing ( Nothing, errors )
Validation Nothing Nothing ( Nothing, errors )
{-| -}
map2 : (a -> b -> c) -> Validation error a named1 -> Validation error b named2 -> Validation error c Never
map2 f (Validation name1 ( maybeParsedA, errorsA )) (Validation name2 ( maybeParsedB, errorsB )) =
map2 f (Validation _ name1 ( maybeParsedA, errorsA )) (Validation _ name2 ( maybeParsedB, errorsB )) =
Validation Nothing
Nothing
( Maybe.map2 f maybeParsedA maybeParsedB
, mergeErrors errorsA errorsB
)
@ -135,7 +137,7 @@ map2 f (Validation name1 ( maybeParsedA, errorsA )) (Validation name2 ( maybePar
{-| -}
fromMaybe : Maybe parsed -> Validation error parsed Never
fromMaybe maybe =
Validation Nothing ( maybe, Dict.empty )
Validation Nothing Nothing ( maybe, Dict.empty )
{-| -}

View File

@ -1,10 +1,20 @@
module Pages.Internal.Form exposing (Named(..), Validation(..))
module Pages.Internal.Form exposing (Named(..), Validation(..), ViewField)
import Dict exposing (Dict)
import Json.Encode as Encode
import Pages.FormState
type Validation error parsed named
= Validation (Maybe String) ( Maybe parsed, Dict String (List error) )
type Validation error parsed kind
= Validation (Maybe (ViewField kind)) (Maybe String) ( Maybe parsed, Dict String (List error) )
{-| -}
type alias ViewField kind =
{ value : Maybe String
, status : Pages.FormState.FieldStatus
, kind : ( kind, List ( String, Encode.Value ) )
}
type Named