Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Custom Error after validating through an API #108

Open
ngw opened this issue Apr 6, 2018 · 0 comments
Open

Custom Error after validating through an API #108

ngw opened this issue Apr 6, 2018 · 0 comments

Comments

@ngw
Copy link

ngw commented Apr 6, 2018

Hello, I'm having problems using elm-form with a custom API to validate one of my fields. My solution ATM "kinda" works but I think that an example on the documentation for this very common situation would be beneficial, once I have this working properly I'll add it to the example and docs.

update : Message -> Model -> (Model, Cmd Message)
update message ({ form } as model) =
  case message of
    ValidateEmailUniqueness response ->
      case response of
        Ok _ ->
          ( { model | emailTaken = False }, Cmd.none )
        Err _ ->
          ( { model | emailTaken = True }, Cmd.none )

This is how I "manually" set a random Boolean on my model, and the part that probably needs updating as it's not using elm-form as it should (it's not using it at all actually).
The HTTP validation is actually triggered using elm-form:

Form.Input "user[email]" Form.Text (Form.Field.String string) ->
  let
    emailField = Form.getFieldAsString "user[email]" form_
    request = emailValidationRequest emailField.value
  in
    case emailField.error of
      Nothing ->
        ({ model | form = form_ }, Http.send ValidateEmailUniqueness request)
      _ ->
        ({ model | form = form_ }, Cmd.none)

I think the main problem here is that I'm not able to put my work into how elm-form validation works, for example here:

(field "user[email]" (email |> andThen nonEmpty))

Main problem obviously is that even when emailTaken is True I can submit the form.
Any suffestion on how to accomplish this?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant