You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+19-6Lines changed: 19 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -19,10 +19,18 @@ You can write a basic Formless form in just a few lines of code. You are respons
19
19
First, a form type that describes the fields in your form, along with their validation error type, user input type, and validated output type. Note: you can provide whatever custom error types you'd like, use `Void` to represent no possible errors, parse to whatever type you want, and none of your fields need to share any types.
20
20
21
21
```purescript
22
+
import Prelude
23
+
import Data.Newtype (class Newtype, unwrap)
24
+
22
25
type Dog = { name :: String, age :: Age }
23
26
24
27
newtype Age = Age Int
25
28
29
+
derive instance newtypeAge :: Newtype Age _
30
+
31
+
instance showAge :: Show Age where
32
+
show = show <<< unwrap
33
+
26
34
data AgeError = TooLow | TooHigh | InvalidInt
27
35
28
36
newtype DogForm r f = DogForm (r
@@ -39,14 +47,17 @@ Next, the component input, which is made up of initial values and validation fun
39
47
You can generate sensible defaults for all input fields in your form by setting `initialInputs` to `Nothing`, or you can manually provide the starting value for each field in your form.
40
48
41
49
```purescript
50
+
import Data.Either (Either(..))
51
+
import Data.Int as Int
52
+
import Data.Maybe (Maybe(..))
42
53
import Formless as F
43
54
44
55
input :: forall m. Monad m => F.Input' DogForm m
45
56
input =
46
57
{ initialInputs: Nothing -- same as: Just (F.wrapInputFields { name: "", age: "" })
47
58
, validators: DogForm
48
59
{ name: F.noValidation
49
-
, age: F.hoistFnE_ \str -> case fromString str of
60
+
, age: F.hoistFnE_ \str -> case Int.fromString str of
50
61
Nothing -> Left InvalidInt
51
62
Just n
52
63
| n < 0 -> Left TooLow
@@ -60,15 +71,15 @@ Finally, the component spec, which is made up of a number of optional functions
60
71
61
72
For our small form, we'll do two things: we'll provide a render function, and when the form is submitted, we'll output a `Dog` to parent components. Along the way we'll wire things up so that input fields display their current value from form state; typing into an input field updates its value in state, also running the correct validation function; we'll display the validation error for `age` if there is one; and we'll wire up a submit button.
62
73
63
-
Note: If you would like to have your form raise no messages (rare), do not supply a `handleMessage` function. If you would like to raise the usual Formless messages (`Changed`, `Submitted`), then provide `H.raise` as your `handleMessage` function. If you would like to simply raise your form's validated output type (`Dog`, in this example), then provide `F.raiseResult` as your `handleMessage` function. Finally, if you want to do something else, you can write a custom function that does whatever you would like.
74
+
Note: If you would like to have your form raise no messages (rare), do not supply a `handleEvent` function. If you would like to raise the usual Formless messages (`Changed`, `Submitted`), then provide `H.raise` as your `handleEvent` function. If you would like to simply raise your form's validated output type (`Dog`, in this example), then provide `F.raiseResult` as your `handleEvent` function. Finally, if you want to do something else, you can write a custom function that does whatever you would like.
64
75
65
76
```purescript
77
+
import Data.Symbol (SProxy(..))
66
78
import Halogen.HTML as HH
67
79
import Halogen.HTML.Events as HE
68
80
import Halogen.HTML.Properties as HP
69
-
import Formless as F
70
81
71
-
spec :: forall m. Monad m => F.Spec' DogForm Dog m
82
+
spec :: forall input m. Monad m => F.Spec' DogForm Dog input m
0 commit comments