Skip to content

Form values with type bool always interpreted as true #838

@OnkV

Description

@OnkV

My System:

  • Debian 12
  • OCaml 5.2.1
  • Ocsigenserver 6.0.0
  • Eliom 11.1.1

This code creates a form which toggles a boolean value (stored in a reference):

(*** Services ***)

let%server test_service =
  let open Eliom_service in
  create ~path:(Path ["test"]) ~meth:(Get Eliom_parameter.unit) ()

let%client test_service = ~%test_service

let%server redir_to_test_service =
  let open Eliom_service in
  create ~path:(Path ["redir_to_test"])
    ~meth:(Get Eliom_parameter.unit) ()

let%client redir_to_test_service = ~%redir_to_test_service

let%server test_form_service =
  let open Eliom_service in
  create_attached_post
    ~fallback:redir_to_test_service
    ~post_params:(Eliom_parameter.(bool "bool_value")) ()

let%client test_form_service = ~%test_form_service

(*** References ***)

let persistent = ...

let%server ref_bool_test : bool Eliom_reference.eref =
  Eliom_reference.eref ~persistent
    ~scope:Eliom_common.default_session_scope
    false

let%rpc set_ref_bool_test (value:bool) =
  Eliom_reference.set ref_bool_test value

let%rpc get_ref_bool_test () =
  Eliom_reference.get ref_bool_test

(*** Handlers ***)

let%shared redir_to_test_handler () () =
  Lwt.return (Eliom_registration.Redirection test_service)

let%shared test_form next_bool_value =
  let open Eliom_content.Html.F in
  let button_value = if next_bool_value then "-> true" else "-> false" in
  Form.post_form ~service:test_form_service
    (fun next_bool_param -> [
         Form.input ~input_type:`Hidden ~name:next_bool_param
           ~value:next_bool_value Form.bool;
         Form.input ~input_type:`Submit ~value:button_value Form.string]) ()

let%shared test_form_handler () next_bool_value =
  set_ref_bool_test next_bool_value

let%shared test_handler () () =
  let open Eliom_content.Html.F in
  let%lwt curr_bool_value = get_ref_bool_test () in
  let next_bool_value = not curr_bool_value in
  Lwt.return (html (head (title (txt "Test")) [])
                (body [h1 [txt "Test"];
                       txt ("current bool value: " ^
                            (string_of_bool curr_bool_value));
                       (test_form next_bool_value)]))

(*** Registration ***)

module%shared Test_app =
  Eliom_registration.App (struct
    let application_name = application_name
    let global_data_path = Some ["__global_data__"]
  end)

let%shared () =
  let open Eliom_registration in
  Test_app.register ~service:test_service test_handler ;
  Redirection.register ~service:redir_to_test_service redir_to_test_handler ;
  Action.register ~service:test_form_service test_form_handler 

I hope you don't mind the elaborate example code.

The HTML generated from the form looks fine. It includes a hidden input field with the correct boolean value - but the form handler always receives true.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions