From 69bde1d010f26dc097ffbf69b284ec8b9e8d28ea Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Mon, 8 Aug 2022 07:50:30 -0700 Subject: [PATCH] Migrate route over to new server-side validation API. --- examples/pokedex/app/Route/TailwindForm.elm | 53 +++++++++++++-------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/examples/pokedex/app/Route/TailwindForm.elm b/examples/pokedex/app/Route/TailwindForm.elm index 6784eb46..4aedaeb5 100644 --- a/examples/pokedex/app/Route/TailwindForm.elm +++ b/examples/pokedex/app/Route/TailwindForm.elm @@ -213,7 +213,7 @@ validateCapitalized string = ( Nothing, [ "Needs to be capitalized" ] ) -form : Form.StyledHtmlForm String User data msg +form : Form.DoneForm String (DataSource (Combined String User)) data (List (Html (Pages.Msg.Msg Msg))) form = Form.init (\first last username email dob checkin checkout rating password passwordConfirmation comments candidates offers pushNotifications acceptTerms -> @@ -256,6 +256,26 @@ form = else Validation.succeed validated ) + |> Validation.andThen + (\clientValidatedForm -> + Validation.map2 + (\dobValue usernameValue -> + isValidDob dobValue + |> DataSource.map + (\maybeError -> + case maybeError of + Nothing -> + Validation.succeed clientValidatedForm + + Just error -> + dob |> Validation.fail error + ) + |> DataSource.map + (Validation.withErrorIf (usernameValue == "asdf") username "username is taken") + ) + dob + username + ) , view = \formState -> let @@ -357,14 +377,6 @@ form = [] ) ) - |> Field.withServerValidation - (\username -> - if username == "asdf" then - DataSource.succeed [ "username is taken" ] - - else - DataSource.succeed [] - ) ) |> Form.field "email" (Field.text @@ -379,14 +391,6 @@ form = |> Field.withMin (Date.fromCalendarDate 1900 Time.Jan 1 |> Form.Value.date) "Choose a later date" |> Field.withMax (Date.fromCalendarDate 2022 Time.Jan 1 |> Form.Value.date) "Choose an earlier date" |> Field.withInitialValue (always defaultUser.birthDay >> Form.Value.date) - |> Field.withServerValidation - (\birthDate -> - if birthDate == Date.fromCalendarDate 1969 Time.Jul 20 then - DataSource.succeed [ "No way, that's when the moon landing happened!" ] - - else - DataSource.succeed [] - ) ) |> Form.field "checkin" (Field.date @@ -445,6 +449,15 @@ form = ) +isValidDob : Date -> DataSource (Maybe String) +isValidDob birthDate = + if birthDate == Date.fromCalendarDate 1969 Time.Jul 20 then + DataSource.succeed (Just "No way, that's when the moon landing happened!") + + else + DataSource.succeed Nothing + + type PushNotificationsSetting = PushAll | PushEmail @@ -537,14 +550,14 @@ route = action : RouteParams -> Parser (DataSource (Response ActionData ErrorPage)) action routeParams = - Request.formData [ form ] + Request.formDataWithServerValidation [ form ] |> Request.map (\toDataSource -> toDataSource |> DataSource.andThen (\result -> case result of - Ok user -> + Ok ( _, user ) -> DataSource.succeed { user = user , flashMessage = @@ -553,7 +566,7 @@ action routeParams = } |> DataSource.map Response.render - Err error -> + Err (Form.Response error) -> DataSource.succeed { flashMessage = Err "Got errors" , user = defaultUser