mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-12-25 04:43:03 +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 )
|
||||
-> ServerForms error parsed
|
||||
-> ( 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
|
||||
firstParser :: remainingParsers ->
|
||||
let
|
||||
@ -918,15 +928,23 @@ runOneOfServerSide rawFormData (ServerForms parsers) =
|
||||
)
|
||||
in
|
||||
case thing of
|
||||
( Just parsed, [] ) ->
|
||||
( Just parsed, Dict.empty )
|
||||
( Just parsed, errors ) ->
|
||||
( Just parsed, errors |> Dict.fromList )
|
||||
|
||||
_ ->
|
||||
runOneOfServerSide rawFormData (ServerForms remainingParsers)
|
||||
( Nothing, errors ) ->
|
||||
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
|
||||
( Nothing, Dict.empty )
|
||||
( Nothing, firstFoundErrors |> Maybe.withDefault [] |> Dict.fromList )
|
||||
|
||||
|
||||
{-| -}
|
||||
|
@ -70,29 +70,31 @@ all =
|
||||
)
|
||||
, describe "oneOf" <|
|
||||
let
|
||||
oneOfParsers : Form.ServerForms String Action
|
||||
oneOfParsers =
|
||||
Form.init
|
||||
(\_ ->
|
||||
{ combine = Validation.succeed ()
|
||||
, view = \_ -> Div
|
||||
}
|
||||
Form.initCombined SetQuantity
|
||||
(Form.init
|
||||
(\_ uuid quantity ->
|
||||
{ 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.initCombined (\() -> Signout)
|
||||
|> Form.combine SetQuantity
|
||||
|> Form.combine (\() -> Signout)
|
||||
(Form.init
|
||||
(\_ uuid quantity ->
|
||||
{ combine =
|
||||
Validation.succeed Tuple.pair
|
||||
|> Validation.andMap (uuid |> Validation.map Uuid)
|
||||
|> Validation.andMap quantity
|
||||
, view =
|
||||
\_ -> Div
|
||||
(\_ ->
|
||||
{ combine = Validation.succeed ()
|
||||
, 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")
|
||||
)
|
||||
in
|
||||
[ test "first branch" <|
|
||||
|
Loading…
Reference in New Issue
Block a user