5

I have a FormPage I am working on that uses CompoundElement to structure the output. In most cases, I want the user to be able to enter both fields, but occasionally I want to have the field hidden while providing the input automatically.

In a normal form, hiding the field but providing input is easy:

FormPage[{"First" -> "String", 
  "Last" -> <|"Interpreter" -> "String", "Input" -> "Jones", 
    "Hidden" -> True|>}, # &]

In the example above, I can enter any First Name and the Last name will return as "Jones".

However, if I use CompoundElement to try and reach a similar result, the field is no longer hidden:

FormPage[{"Name:" -> 
   CompoundElement[<|"First" -> "String", 
     "Last" -> <|"Interpreter" -> "String", "Input" -> "Jones", 
       "Hidden" -> True|>|>]}, # &]

Form Example

The actual form I am working on is much more complex, hence the need for CompoundElement but the above code isolates the problem.

I have scoured the documentation for CompoundElement and while it is included as an option in the FormPage write ups, the focus is more on structuring data in the supporting documents. Perhaps there is an option I can use in defining the form appearance?

UPDATE: This quirk has been corrected in v12.0 and later.

kickert
  • 1,820
  • 8
  • 22

1 Answers1

4

Here's a convenient function for Form* (and other cloud stuff) debugging:

testForm[form_] :=

 With[{xml = 
    Import[Forms`GenerateHTTPResponse`PackagePrivate`handleExport[
      form], {"HTML", "XMLObject"}]},
  FirstCase[
    xml,
    XMLElement["form", _, _],
    FirstCase[
     xml,
     XMLElement["div", {___, "id" -> "content", ___}, _],
     None,
     ∞
     ],
    ∞
    ] // StringReplace[ExportString[#, "XML"], 
     Repeated["\n" ~~ (" " ..), {2, ∞}] -> "\n"] &
  ]

Now if we look at the generated text we can see "Last" is wholly absent from the first but present in the second:

testForm@FormPage[{"First" -> "String", 
    "Last" -> <|"Interpreter" -> "String", "Input" -> "Jones", 
      "Hidden" -> True|>}, # &] // StringContainsQ["Last"]

False

testForm@FormPage[{"Name:" -> 
     CompoundElement[<|"First" -> "String", 
       "Last" -> <|"Interpreter" -> "String", "Input" -> "Jones", 
         "Hidden" -> True|>|>]}, # &] // StringContainsQ["Last"]

True

This suggests it's just a bug with the processing of CompoundElement. I'd report it.

The only real workarounds will be hacks, since you'd need to trick the HTML exporter into adding styles to your elements to hide them with CSS or find a way .

Even using that export trick I used as a pre-processor step and then adding the HTML in post-processing doesn't seem to work, as the static HTML pages generated appear to not support the callback properly.

If you want to figure out exactly how to edit your FormPage to work I'd suggest looking into Templating`PackageScope`toHTMLNode as it's the apparent work-horse for these export processes.

b3m2a1
  • 46,870
  • 3
  • 92
  • 239
  • Thanks @b3m2a1. Using your function, I dug a bit more into the HTML being exported and you are absolutely right -- things are being processed much differently. Not sure how much I want to go digging under the hood to fix a relatively minor problem, but as I build more complicated forms, that may need to the route I go. – kickert Nov 07 '18 at 19:05