How to write forms?

To write an HTML form towards an Eliom service

Just as we do for links, we provide form-building functions that call Eliom services in a safe manner. These functions are provided in the module Eliom_content.​Html5.​D.​Form (and Eliom_content.​Html5.​F.​Form). The functions Eliom_content.​Html5.​D.​Form.​get_form and Eliom_content.​Html5.​D.​Form.​post_form accept a function (named create_form in our example below) which is responsible for generating the content from appropriately-typed parameter names.

let create_form =
  (fun (number_name, (number2_name, string_name)) ->
    [p [pcdata "Write an int: ";
          ~input_type:`Text ~name:number_name
        pcdata "Write another int: ";
          ~input_type:`Text ~name:number2_name
        pcdata "Write a string: ";
        Html5.D.Form.input ~input_type:`Text ~name:string_name Form.string;
        Html5.D.Form.input ~input_type:`Submit ~value:"Click" Form.string]])

let form = register_service ["form"] unit
  (fun () () ->
     let f = Html5.D.Form.get_form coucou_params create_form in
         (head (title (pcdata "")) [])
         (body [f])))

As shown in the example, Eliom_content.​Html5.​D.​Form provides functions for generating the various widgets, e.g., Eliom_content.​Html5.​D.​Form.​input. These functions need to be called with a "type" argument (e.g., Eliom_content.​Html5.​D.​Form.​int) that matches the type of the corresponding parameter name.

POST forms may also take get parameters, as last argument of function post_form:

  (fun chaine ->
    p [pcdata "Write a string: ";
       Form.input ~input_type:`Text ~name:chaine Form.string])

Raw forms

There is also a raw interface to write basic forms without Eliom, using standard tyxml constructors.

Use module Eliom_content.Html5.D.Raw.