Message Boards Message Boards

Validation in WebForm before next is pressed

Posted 9 years ago

Here is a 2-step webform. After the first step I need validation. v1x, v1y, v1z must be different numbers. If for example v1x and v1z contain the same number I need an error message to appear.

here is a 2 step form:

CloudDeploy[
 FormFunction[
  {FormObject[
     <|"v1x" -> 
      <|"Interpreter" -> {"Most" -> 1, "Least" -> 2, "Excluded" -> 0},
       "Control" -> RadioButtonBar, "Default" -> 0|> ,
     "v1y" -> 
      <|"Interpreter" -> {"Most" -> 1, "Least" -> 2, "Excluded" -> 0},
       "Control" -> RadioButtonBar, "Default" -> 0|> , 
     "v1z" -> 
      <|"Interpreter" -> {"Most" -> 1, "Least" -> 2, "Excluded" -> 0},
       "Control" -> RadioButtonBar,  "Default" -> 0|> 
     |>],
   FormObject[
    <|"v2x" -> 
      <|"Interpreter" -> {"Most" -> 1, "Least" -> 2, "Excluded" -> 0},
       "Control" -> RadioButtonBar, "Default" -> 0|> ,
     "v2y" -> 
      <|"Interpreter" -> {"Most" -> 1, "Least" -> 2, "Excluded" -> 0},
       "Control" -> RadioButtonBar, "Default" -> 0|> , 
     "v2z" -> 
      <|"Interpreter" -> {"Most" -> 1, "Least" -> 2, "Excluded" -> 0},
       "Control" -> RadioButtonBar,  "Default" -> 0|> 
     |>]
   }
  , Identity],
 "test"]

I did have studied this approach but this only provides validation for 1 var each time. I need to compare 3 values per step in the form before continuing to the next step.

toObject[s_String] /; ToUpperCase[s] == s := myObject[s];
toObject[s_] := 
  Failure["InterpretationFailure", <|
    "MessageTemplate" -> "please insert an uppercased string"|>];
CloudDeploy [
 FormFunction[FormObject[{"x" -> toObject}], Identity], "/test"]

suggestions ?

2 Replies

Chad,
Forgot to thank you for this answer. Worked really nice. I was able to generate a 90 questions webform questionnaire. The only thing I learned is not to use pictures in a multipage webform FormLayoutFunction (slow "next" response). The picture code expression gets multiplied by the number of steps it has. Its better to ling using IMG tags to pictures on another website.

Since there are only 3 "ordinates" with 3 choices each, there are only 6 possible permutations for each variable (v1 and v2). So how about a different type of form that doesn't require any checks on the selected values? Something like below. Note that my form labels are ugly. I'm sure you could come up with a nicer way to present the options.

In[26]:= perms = Permutations[{0, 1, 2}];
In[27]:= labels = Permutations[{"Most", "Least", "Excluded"}];
dir = {"x=", "y=", "z="};
opt = dir[[1]] <> #[[1]] <> " " <> dir[[2]] <> #[[2]] <> " " <> dir[[3]] <> #[[3]] & /@ labels

In[31]:= FormFunction[{FormObject[<|
     "v1" -> <| "Interpreter" -> 
        Table[opt[[i]] -> perms[[i]], {i, Length@perms}], 
       "Control" -> RadioButtonBar, "Default" -> perms[[1]]|>|>], 
   FormObject[<|
     "v2" -> <| "Interpreter" -> 
        Table[opt[[i]] -> perms[[i]], {i, Length@perms}], 
       "Control" -> RadioButtonBar, "Default" -> perms[[1]], 
       "Control" -> RadioButtonBar, "Default" -> 0|>|>]}, Identity ][]

Out[31]= <|"v1" -> {0, 2, 1}, "v2" -> {2, 1, 0}|>
POSTED BY: Chad Knutson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract