Very beginner question here:
This code makes the barest minimum form, now to add more text boxes to the form, how would I do this in the most generic way possible, so that the same function can be used to update many text boxes.
It is this function I don’t want to repeat 10 times for multiple text boxes: “let first_name str = FirstName str in”
and in the update function I am somewhat unsure exactly what I am doing. If I add a last name text box with a similar update function for the new field i get a compiler error.
(* file: form.ml *)
open Fmlib_browser
type state =
{
first_name: string;
}
let init: state =
{
first_name = "";
}
type msg =
| FirstName of string
let view state =
let open Html in
let open Attribute in
let first_name str = FirstName str in
let section attrs nodes = node "section" attrs nodes in
let form attrs nodes = node "form" attrs nodes in
div [class_ "container"] [
section [id "main"] [
form [] [
section [id "details"] [
div [class_ "grid"] [
label [] [text "First Name"; input [
attribute "type" "text"
; value state.first_name
; on_input first_name] []]
]
]
]
; p [] [
text "first name: "
; text (state.first_name) ]
]
]
let update state = function
| FirstName first_name ->
{first_name}
let _ =
sandbox (* very simple applications are sandbox applications *)
init (* initial state *)
view (* view function *)
update (* update function *)
Kindness
David