1
$\begingroup$

I am building a Form to capture several non-zero numbers as inputs. The basic code is:

Ratesform =
  FormFunction[{"DriftRates", ""} -> RepeatingElement[
        <|"Interpreter" ->  Interpreter["Number", 
        Function[x, x != 0]],  
        "Hint" -> "Non-zero values only", 
        "Label" :> "Drift rate " <> ToString[i]|>, {{i, 2}, {1, 10}}], 
        AppearanceRules -> <|"Title" -> "Enter the possible drift rates", 
        "SubmitLabel" -> "Submit"|>][]; 

This produces a form that only accepts non-zero values. But, if the user does enter a 0, it provides the Help "Enter a valid value." I want to override that to read "Enter a non-zero value".

Help for 0 entry

Given the value 0, the Interpreter returns a Failure Object with Message: "Enter a valid value.", Tag: ConditionFailure Input: 0 Type: Number

Interpreter's Failure

So I figured adding a Failure option to the Interpreter would do the trick:

Ratesform =
  FormFunction[{"DriftRates", ""} -> RepeatingElement[
        <|"Interpreter" ->  Interpreter["Number", 
        Function[x, x != 0], 
        Failure["ConditionFailure", 
        <|"MessageTemplate"-> StringTemplate["Enter a non-zero number"]|>]],  
        "Hint" -> "Non-zero values only", 
        "Label" :> "Drift rate " <> ToString[i]|>, {{i, 2}, {1, 10}}], 
        AppearanceRules -> <|"Title" -> "Enter the possible drift rates", 
        "SubmitLabel" -> "Submit"|>][]; 

But that provides no help at all. How to override the default Failure?

$\endgroup$
1
$\begingroup$

The failure argument of the Interpreter call must be a function, so just add an & at the end of that argument. Like so.

Ratesform =
  FormFunction[
    {"DriftRates", ""} ->
      RepeatingElement[
        <|"Interpreter" ->
            Interpreter[
              "Number", # != 0 &,
              (* the following arg must be a function *)
              Failure[
                "ConditionFailure", 
                <|"MessageTemplate" -> 
                    StringTemplate["Enter a non-zero number"]|>] &],
          "Hint" -> "Non-zero values only", 
          "Label" :> "Drift rate " <> ToString[i]|>, {{i, 2}, {1, 10}}], 
    AppearanceRules ->
      <|"Title" -> "Enter the possible drift rates", 
        "SubmitLabel" -> "Submit"|>][]

form

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.