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

Nesting a multiple field validation loses the error #100

Closed
nmk opened this issue Dec 13, 2017 · 2 comments
Closed

Nesting a multiple field validation loses the error #100

nmk opened this issue Dec 13, 2017 · 2 comments

Comments

@nmk
Copy link

nmk commented Dec 13, 2017

I am trying to write a validation which checks that two fields together satisfy some condition.

Here is a working example: https://ellie-app.com/mjLjf9kDma1/3

Select One and Two from the dropdowns and the validation succeeds. Select One and Three and the validation fails and the error is displayed.

However, when I embed this validation in another one, the validation still fails, yet the error is lost, resulting in getOutput f == Nothing without errors. Here is an example: https://ellie-app.com/mjLjf9kDma1/4

I suspect this is a grouping issue, but I cannot figure out what the correct expression grouping in the second example should be. I would greatly appreciate any help with this one!

@etaque
Copy link
Owner

etaque commented Dec 19, 2017

Hello,
Finally found the time to look at your issue, it's effectively a grouping problem.

In there:

thingCombinationValidation : Validation MyError ( Thing, Thing )
thingCombinationValidation =
    field "firstThing" thingValidation
        |> andThen
            (\fst ->
                field "secondThing" thingValidation
                    |> andThen
                        (\snd ->
                            if List.member ( fst, snd ) forbiddenCombinations then
                                fail (customError InvalidCombination)
                            else
                                succeed ( fst, snd )
                        )
            )

The fail (customError InvalidCombination) isn't attached to a field, like that, so error is lost in errors tree, see how it's done in lib:
https://github.com/etaque/elm-form/blob/master/src/Form/Validate.elm#L131

So what you can do is to use field to attach your custom error, like so:

field "firstThing" (fail (customError InvalidCombination))

Maybe that kind of error could be caught by better types. Worth investigating!

@nmk
Copy link
Author

nmk commented Feb 2, 2018

Thank you for looking into this! I was pretty sure I had answered during the holidays, but just noticed I hadn't. Sorry about that!

Using field to add an error works, but is not really intuitive. Maybe adding a function like failAtField implemented in terms of field and fail as shown above would make it clearer.

@nmk nmk closed this as completed Feb 2, 2018
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

2 participants