0

Consider the following code that sets the background of the numeric input widget to red upon invalid input:

library(shiny)

ui <- shinyUI(
    fluidPage(
        column(12,
               tags$style(HTML("input:invalid {background-color: #FFCCCC;}")),
               numericInput("test", h5("Test value:"), value = 6, min = 0, max = 10, step = 2, width = '200px')
        )
    )
)

server <- function(input, output) {
}

shinyApp(ui = ui, server = server)

If the user enters '-1', or '11', or '4', then the code works as expected (invalid for the first two examples, valid for the third example).

However, observe what happens when an odd number is entered that is between the min and max values. The code thinks the input is invalid. But notice that the 'step' value to the numeric input is even, suggesting that the code mistakenly thinks that a correct number must not only be between 0 and 10, but also a multiple of 2.

To prove this, remove the 'step' option, or change it to 1, and then any (integer-valued) input between 0 and 10 will work.

Is this a bug, and is there a workaround (that is, so that I can maintain the 'step' value but still allow any and all numbers between the min and max), or am I missing something here?

I was under the impression that the 'step' value simply controlled the amount by which the arrows increment or decrement input, not for the purposes of input validation.

qanda
  • 225
  • 3
  • 12
  • It's not a bug of course, it's just HTML. The `step` attribute doc: https://www.w3schools.com/tags/att_input_step.asp. Quoting from there: `Example: if step="3", legal numbers could be -3, 0, 3, 6, etc.`. – nicola May 13 '18 at 04:52
  • So do you know how I can keep the step functionality but allow validity of any number within min and max? – qanda May 13 '18 at 05:54
  • Consider that the value, be it valid or invalid, gets passed to the server and you can do whatever you want with it. So, as long as you remove the red highlight when an invalid number is entered, you can't tell an invalid input from a valid one. – nicola May 13 '18 at 09:34
  • That being the case then, how can I keep the invalid-identifying red highlighting from only those numbers outside the range, but remove for any numbers inside the range, whether a multiple of the step value or not? – qanda May 13 '18 at 09:50

0 Answers0