js_of_ocaml icon indicating copy to clipboard operation
js_of_ocaml copied to clipboard

[FEATURE REQUEST] Are you open to a PR to extend Form with other functions ?

Open RuyBlast opened this issue 5 years ago • 1 comments

Is your feature request related to a problem? Please describe. I'm frustrated with form API: it is great to collect all data, but it does not allow one to make field content validation at the field level.

Describe the solution you'd like I would like to expose an intermediate function that gets all html elements from a form, and another that would extract the content of one submitable html element (which would allow custom form validation at the field level). It would look like:

let form_fields (form : formElement t) =
  let length = form##.elements##.length in
  Array.to_list (Array.init length (fun i -> Opt.to_option (form##.elements##item i)))

let field_content ?get = function
           | None -> [] (* shouldn't happen *)
           | Some v -> (
               match tagged v with
               | Select v -> get_select_val v
               | Input v -> get_input_val ?get v
               | Textarea v -> get_textarea_val v
               | _ -> [])

Describe alternatives you've considered form_fields is easy to write so it is not compulsory, but field_content is difficult to write without get_select_val, get_input_val and get_textarea_val which are not exposed. Another solution would be to expose only those three functions in according mli file. My last alternative would be to make my own modification of Form API and install accordingly modified Js_of_ocaml library from source, but I think the ability of making custom form validation would benefit other people.

Thanks for your answer and your work on this great library !

RuyBlast avatar Apr 19 '20 16:04 RuyBlast

@RuyBlast, sorry for this long silence. Is this still something that you care about ? Is it impractical to do validation using val form_elements : ?get:bool -> Dom_html.formElement t -> (string * form_elt) list ?

I've just done some small refactoring in #1218

hhugo avatar Jan 11 '22 10:01 hhugo

Reopen if this is still relevent

hhugo avatar Nov 10 '22 13:11 hhugo