mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-11-25 09:21:57 +03:00
Include a view function in form parser.
This commit is contained in:
parent
4f5bbce442
commit
5e9275ec4c
@ -70,11 +70,13 @@ requiredString error =
|
||||
)
|
||||
|
||||
|
||||
andThenNew : a -> CombinedParser String a
|
||||
andThenNew fn =
|
||||
andThenNew : combined -> viewFn -> CombinedParser String combined viewFn
|
||||
andThenNew fn viewFn =
|
||||
CombinedParser []
|
||||
(\formState ->
|
||||
( Just fn, Dict.empty )
|
||||
{ result = ( Just fn, Dict.empty )
|
||||
, view = viewFn
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@ -94,8 +96,8 @@ andThenNew fn =
|
||||
field :
|
||||
String
|
||||
-> FieldThing error parsed
|
||||
-> CombinedParser error (ParsedField error parsed -> a)
|
||||
-> CombinedParser error a
|
||||
-> CombinedParser error (ParsedField error parsed -> combined) (RawField -> combinedView)
|
||||
-> CombinedParser error combined combinedView
|
||||
field name (FieldThing fieldParser) (CombinedParser definitions parseFn) =
|
||||
CombinedParser
|
||||
(( name, FieldDefinition )
|
||||
@ -118,22 +120,47 @@ field name (FieldThing fieldParser) (CombinedParser definitions parseFn) =
|
||||
}
|
||||
)
|
||||
|
||||
myFn :
|
||||
( Maybe (ParsedField error parsed -> a)
|
||||
, Dict String (List error)
|
||||
)
|
||||
-> ( Maybe a, Dict String (List error) )
|
||||
myFn ( fieldThings, errorsSoFar ) =
|
||||
( case fieldThings of
|
||||
Just fieldPipelineFn ->
|
||||
parsedField
|
||||
|> Maybe.map fieldPipelineFn
|
||||
rawField : RawField
|
||||
rawField =
|
||||
{ name = name
|
||||
, value = formState |> Dict.get name |> Maybe.map .value
|
||||
}
|
||||
|
||||
Nothing ->
|
||||
Nothing
|
||||
, errorsSoFar
|
||||
|> addErrors name errors
|
||||
)
|
||||
--{ result :
|
||||
-- ( Maybe parsed
|
||||
-- , Dict String (List error)
|
||||
-- )
|
||||
--, view : view
|
||||
--}
|
||||
myFn :
|
||||
{ result :
|
||||
( Maybe (ParsedField error parsed -> combined)
|
||||
, Dict String (List error)
|
||||
)
|
||||
, view : RawField -> combinedView
|
||||
}
|
||||
->
|
||||
{ result : ( Maybe combined, Dict String (List error) )
|
||||
, view : combinedView
|
||||
}
|
||||
myFn soFar =
|
||||
let
|
||||
( fieldThings, errorsSoFar ) =
|
||||
soFar.result
|
||||
in
|
||||
{ result =
|
||||
( case fieldThings of
|
||||
Just fieldPipelineFn ->
|
||||
parsedField
|
||||
|> Maybe.map fieldPipelineFn
|
||||
|
||||
Nothing ->
|
||||
Nothing
|
||||
, errorsSoFar
|
||||
|> addErrors name errors
|
||||
)
|
||||
, view = soFar.view rawField
|
||||
}
|
||||
in
|
||||
formState
|
||||
|> parseFn
|
||||
@ -188,14 +215,30 @@ input attrs fieldThing =
|
||||
[]
|
||||
|
||||
|
||||
runNew : Form.FormState -> CombinedParser error parsed -> ( Maybe parsed, Dict String (List error) )
|
||||
runNew :
|
||||
Form.FormState
|
||||
-> CombinedParser error parsed view
|
||||
->
|
||||
{ result : ( Maybe parsed, Dict String (List error) )
|
||||
, view : view
|
||||
}
|
||||
runNew formState (CombinedParser fieldDefinitions parser) =
|
||||
--Debug.todo ""
|
||||
parser formState
|
||||
|
||||
|
||||
type CombinedParser error parsed
|
||||
= CombinedParser (List ( String, FieldDefinition )) (Form.FormState -> ( Maybe parsed, Dict String (List error) ))
|
||||
type CombinedParser error parsed view
|
||||
= CombinedParser
|
||||
(List ( String, FieldDefinition ))
|
||||
(Form.FormState
|
||||
->
|
||||
{ result :
|
||||
( Maybe parsed
|
||||
, Dict String (List error)
|
||||
)
|
||||
, view : view
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
|
||||
@ -253,6 +296,12 @@ type alias ParsedField error parsed =
|
||||
}
|
||||
|
||||
|
||||
type alias RawField =
|
||||
{ name : String
|
||||
, value : Maybe String
|
||||
}
|
||||
|
||||
|
||||
value : FullFieldThing error parsed -> parsed
|
||||
value =
|
||||
Debug.todo ""
|
||||
|
@ -51,34 +51,34 @@ all =
|
||||
-- [ ( "password-confirmation", [ "Must match password" ] )
|
||||
-- ]
|
||||
-- )
|
||||
test "non-dependent field error" <|
|
||||
\() ->
|
||||
FormParser.runNew
|
||||
(fields
|
||||
[ ( "password", "mypassword" )
|
||||
, ( "password-confirmation", "" )
|
||||
]
|
||||
)
|
||||
(FormParser.andThenNew
|
||||
(\password passwordConfirmation ->
|
||||
if password.value == passwordConfirmation.value then
|
||||
--passwordConfirmation |> FormParser.withError "Must match password"
|
||||
Debug.todo ""
|
||||
|
||||
else
|
||||
FormParser.ok { password = password }
|
||||
)
|
||||
|> FormParser.field "password" (FormParser.requiredString "Password is required")
|
||||
|> FormParser.field "password-confirmation" (FormParser.requiredString "Password confirmation is required")
|
||||
)
|
||||
|> Expect.equal
|
||||
( Nothing
|
||||
, Dict.fromList
|
||||
[ ( "password", [] )
|
||||
, ( "password-confirmation", [ "Password confirmation is required" ] )
|
||||
]
|
||||
)
|
||||
, test "new design idea 3" <|
|
||||
--test "non-dependent field error" <|
|
||||
-- \() ->
|
||||
-- FormParser.runNew
|
||||
-- (fields
|
||||
-- [ ( "password", "mypassword" )
|
||||
-- , ( "password-confirmation", "" )
|
||||
-- ]
|
||||
-- )
|
||||
-- (FormParser.andThenNew
|
||||
-- (\password passwordConfirmation ->
|
||||
-- if password.value == passwordConfirmation.value then
|
||||
-- --passwordConfirmation |> FormParser.withError "Must match password"
|
||||
-- Debug.todo ""
|
||||
--
|
||||
-- else
|
||||
-- FormParser.ok { password = password }
|
||||
-- )
|
||||
-- |> FormParser.field "password" (FormParser.requiredString "Password is required")
|
||||
-- |> FormParser.field "password-confirmation" (FormParser.requiredString "Password confirmation is required")
|
||||
-- )
|
||||
-- |> Expect.equal
|
||||
-- ( Nothing
|
||||
-- , Dict.fromList
|
||||
-- [ ( "password", [] )
|
||||
-- , ( "password-confirmation", [ "Password confirmation is required" ] )
|
||||
-- ]
|
||||
-- ),
|
||||
test "new design idea 3" <|
|
||||
\() ->
|
||||
FormParser.runNew
|
||||
(fields
|
||||
@ -88,29 +88,43 @@ all =
|
||||
)
|
||||
(FormParser.andThenNew
|
||||
(\password passwordConfirmation ->
|
||||
--{
|
||||
--dependentErrors =
|
||||
if password.value /= passwordConfirmation.value then
|
||||
Debug.todo ""
|
||||
--passwordConfirmation |> FormParser.withError "Must match password"
|
||||
|
||||
else
|
||||
{ password = password.value }
|
||||
--FormParser.ok
|
||||
--, view =
|
||||
-- Html.form []
|
||||
-- [ password |> FormParser.input []
|
||||
-- , passwordConfirmation |> FormParser.input []
|
||||
-- ]
|
||||
FormParser.ok { password = password.value }
|
||||
)
|
||||
(\password passwordConfirmation ->
|
||||
Div
|
||||
--Html.form []
|
||||
-- [ password |> FormParser.input []
|
||||
-- , passwordConfirmation |> FormParser.input []
|
||||
-- ]
|
||||
--}
|
||||
)
|
||||
|> FormParser.field "password" (FormParser.string "Password is required")
|
||||
|> FormParser.field "password-confirmation" (FormParser.string "Password confirmation is required")
|
||||
)
|
||||
|> expectNoErrors { password = "mypassword" }
|
||||
|> Expect.equal
|
||||
{ result =
|
||||
( Just { password = "mypassword" }
|
||||
, Dict.fromList
|
||||
[ ( "password", [] )
|
||||
, ( "password-confirmation", [] )
|
||||
]
|
||||
)
|
||||
, view = Div
|
||||
}
|
||||
|
||||
--|> expectNoErrors { password = "mypassword" }
|
||||
]
|
||||
|
||||
|
||||
type MyView
|
||||
= Div
|
||||
|
||||
|
||||
expectNoErrors : parsed -> ( Maybe parsed, Dict String (List error) ) -> Expect.Expectation
|
||||
expectNoErrors parsed =
|
||||
Expect.all
|
||||
|
Loading…
Reference in New Issue
Block a user