mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-12-25 12:52:27 +03:00
Update form parsing logic with new server form parsers.
This commit is contained in:
parent
a106496052
commit
255cd8c6a4
30
src/Form.elm
30
src/Form.elm
@ -903,7 +903,17 @@ runOneOfServerSide :
|
|||||||
List ( String, String )
|
List ( String, String )
|
||||||
-> ServerForms error parsed
|
-> ServerForms error parsed
|
||||||
-> ( Maybe parsed, Dict String (List error) )
|
-> ( Maybe parsed, Dict String (List error) )
|
||||||
runOneOfServerSide rawFormData (ServerForms parsers) =
|
runOneOfServerSide rawFormData forms =
|
||||||
|
runOneOfServerSideHelp rawFormData Nothing forms
|
||||||
|
|
||||||
|
|
||||||
|
{-| -}
|
||||||
|
runOneOfServerSideHelp :
|
||||||
|
List ( String, String )
|
||||||
|
-> Maybe (List ( String, List error ))
|
||||||
|
-> ServerForms error parsed
|
||||||
|
-> ( Maybe parsed, Dict String (List error) )
|
||||||
|
runOneOfServerSideHelp rawFormData firstFoundErrors (ServerForms parsers) =
|
||||||
case parsers of
|
case parsers of
|
||||||
firstParser :: remainingParsers ->
|
firstParser :: remainingParsers ->
|
||||||
let
|
let
|
||||||
@ -918,15 +928,23 @@ runOneOfServerSide rawFormData (ServerForms parsers) =
|
|||||||
)
|
)
|
||||||
in
|
in
|
||||||
case thing of
|
case thing of
|
||||||
( Just parsed, [] ) ->
|
( Just parsed, errors ) ->
|
||||||
( Just parsed, Dict.empty )
|
( Just parsed, errors |> Dict.fromList )
|
||||||
|
|
||||||
_ ->
|
( Nothing, errors ) ->
|
||||||
runOneOfServerSide rawFormData (ServerForms remainingParsers)
|
runOneOfServerSideHelp rawFormData
|
||||||
|
(firstFoundErrors
|
||||||
|
-- TODO is this logic what we want here? Might need to think through the semantics a bit more
|
||||||
|
-- of which errors to parse into - could be the first errors, the last, or some other way of
|
||||||
|
-- having higher precedence for deciding which form should be used
|
||||||
|
|> Maybe.withDefault errors
|
||||||
|
|> Just
|
||||||
|
)
|
||||||
|
(ServerForms remainingParsers)
|
||||||
|
|
||||||
[] ->
|
[] ->
|
||||||
-- TODO need to pass errors
|
-- TODO need to pass errors
|
||||||
( Nothing, Dict.empty )
|
( Nothing, firstFoundErrors |> Maybe.withDefault [] |> Dict.fromList )
|
||||||
|
|
||||||
|
|
||||||
{-| -}
|
{-| -}
|
||||||
|
@ -70,29 +70,31 @@ all =
|
|||||||
)
|
)
|
||||||
, describe "oneOf" <|
|
, describe "oneOf" <|
|
||||||
let
|
let
|
||||||
|
oneOfParsers : Form.ServerForms String Action
|
||||||
oneOfParsers =
|
oneOfParsers =
|
||||||
Form.init
|
Form.initCombined SetQuantity
|
||||||
(\_ ->
|
(Form.init
|
||||||
{ combine = Validation.succeed ()
|
(\_ uuid quantity ->
|
||||||
, view = \_ -> Div
|
{ combine =
|
||||||
}
|
Validation.succeed Tuple.pair
|
||||||
|
|> Validation.andMap (uuid |> Validation.map Uuid)
|
||||||
|
|> Validation.andMap quantity
|
||||||
|
, view =
|
||||||
|
\_ -> Div
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|> Form.hiddenField "kind" (Field.exactValue "setQuantity" "Expected setQuantity")
|
||||||
|
|> Form.hiddenField "uuid" (Field.text |> Field.required "Required")
|
||||||
|
|> Form.field "quantity" (Field.int { invalid = \_ -> "Expected int" } |> Field.required "Required")
|
||||||
)
|
)
|
||||||
|> Form.hiddenField "kind" (Field.exactValue "signout" "Expected signout")
|
|> Form.combine (\() -> Signout)
|
||||||
|> Form.initCombined (\() -> Signout)
|
|
||||||
|> Form.combine SetQuantity
|
|
||||||
(Form.init
|
(Form.init
|
||||||
(\_ uuid quantity ->
|
(\_ ->
|
||||||
{ combine =
|
{ combine = Validation.succeed ()
|
||||||
Validation.succeed Tuple.pair
|
, view = \_ -> Div
|
||||||
|> Validation.andMap (uuid |> Validation.map Uuid)
|
|
||||||
|> Validation.andMap quantity
|
|
||||||
, view =
|
|
||||||
\_ -> Div
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|> Form.hiddenField "kind" (Field.exactValue "setQuantity" "Expected setQuantity")
|
|> Form.hiddenField "kind" (Field.exactValue "signout" "Expected signout")
|
||||||
|> Form.hiddenField "uuid" (Field.text |> Field.required "Required")
|
|
||||||
|> Form.field "quantity" (Field.int { invalid = \_ -> "Expected int" } |> Field.required "Required")
|
|
||||||
)
|
)
|
||||||
in
|
in
|
||||||
[ test "first branch" <|
|
[ test "first branch" <|
|
||||||
|
Loading…
Reference in New Issue
Block a user