I'm not sure there's a nice solution for this but there's one I could come up with. Haven't run the code but it should work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
let _x = Var.Create ()

Form.Return (fun x y -> (x, y))
<*> Form.Yield ""
<*> Form.Yield ""
|> Form.WithSubmit
|> Form.MapToResult(fun (a, b) ->
    Result.Failure [ErrorMessage.Create(Var.GetId _x, "error")])
|> Form.TransmitView
|> Form.Render (fun a b c d ->
    let errros =
        d.Map(function
            | Failure ems -> ems |> List.filter (fun em -> em.Id = Var.GetId _x)
            | Success _ -> [])
    // render errors
)

Instead of Result.FailWith we use Result.Failure with our own ErrorMessage. Each error message has an id which indicates where it came from. Since Forms uses UI.Next Vars as backing fields, the ids are that of the vars which are given when a new var is created (a global counter is increased). Therefore we create a dummy var just for the sake of getting a unique (among vars) id and we use that as the source of our own error message to avoid any type of accidental id clashes (you could technically use a very big number and there's not much chance you will have as many vars as to have a clash with that but just to make sure). Later we can filter by this id.

By on 2/1/2016 9:57 AM ()

Hi Istvan, thanks for your answer. I don't really see the value of using the external Var. If I use a Var<string> to hold my error message and display it on a div, would that make a difference?

Also, filtering is working but d.Through(_x) does not seem to work.

By on 2/2/2016 8:16 AM ()

Hm, maybe WebSharper.Forms still does something funky in Through that I didn't catch.

Well technically yes, you could create a Var<string> and store the error in that but I feel this is more idiomatic. For one, we use one less mutable variable which is always good since we only use the Var<unit> for the sake of getting a unique id. You could just do Result.Failure [ErrorMessage.Create(926, "error")]) and then filter for 926 later and probably you could get away with it but using a dummy var ensures that there will be absolutely no problems in the future in this regard.

By on 2/2/2016 8:59 AM ()

Yes, this is what I ended up doing thanks!

By on 2/2/2016 9:12 AM ()

Actually, it just occured to me you could use d.Through(_x) if you do this as that does the same thing (filters by var id).

By on 2/1/2016 4:50 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper