0

Clarification for @ismirsehregal

If I set num_min to zero in Field A and then increment Field B then Field A shows the red "!" even though zero is valid.

enter image description here

Original Question

Both fields require numbers >=0 and < 300 Empty fields, spaces, ".", "e" are not allowed. When a field is invalid a red "!" should appear AND the button should be disabled.

The code below works for non_numeric characters. I had to make the minimum of the field = -1 to make the disable button work.

Right now it's possible to use the arrows to go down to as low as -1 which is an invalid number.

Is it possible to get both the red "!" AND the disabled button to work if the valid numbers are: 0 <= x < 300

library("shiny")
library("bslib")
library("shinyjs")
library("shinyWidgets")
library("shinyvalidate")

per_month <- "/mth"
num_min <- -1
num_max <- 300

ui <- bootstrapPage(
  useShinyjs(),
  

  theme = bs_theme(version = 5, "font_scale" = 1.0), 
  div(class = "container-fluid",
      
      div(class = "row",
          div(class="col-4", 
              numericInputIcon(
                inputId = "a",
                label = "A",
                value = 100,
                min = num_min,
                max = num_max,
                width = "200px",
                icon = list(NULL, per_month)
              ),
          ),
          div(class="col-4", 
              numericInputIcon(
                inputId = "b",
                label = "B",
                value = 200,
                min = num_min,
                max = num_max,
                width = "200px",
                icon = list(NULL, per_month)
              ),
          ),
          htmlOutput("a")
      ), 
      div(class = "row",
          div(class = "col-12",
              actionButton("cc_calculate", "—— Submit ——")
          ), align = "center"
      )
  )
)

server <- function(input, output, session) {

  output$a <- renderText({
    paste("a:", input$a)
  })
  
       
  iv <- InputValidator$new()

  iv$add_rule("a", sv_required(message = ""))
  iv$add_rule("a", sv_between(num_min, num_max, message_fmt = "", inclusive = c(FALSE, FALSE)))

  iv$add_rule("b", sv_required(message = ""))
  iv$add_rule("b", sv_between(num_min, num_max, message_fmt = "", inclusive = c(FALSE, FALSE)))

  iv$enable()

  observe(
    if (iv$is_valid()) {
      enable("cc_calculate")
    } else {
      disable("cc_calculate")
    }
  )

}

shinyApp(ui = ui, server = server)
ixodid
  • 2,180
  • 1
  • 19
  • 46
  • Seems to be working fine - also with `num_min <- 0` – ismirsehregal Mar 17 '22 at 09:00
  • Please see new info at top of question. I don't think your solution works. Also, all my packages are up to date. – ixodid Mar 17 '22 at 20:08
  • My solution? I didn't provide a solution. Why should 0 be valid if you set `inclusive = FALSE`? – ismirsehregal Mar 17 '22 at 20:41
  • OK. "My" solution does not work regardless of whether inclusive = TRUE or FALSE. Or num_min <- -1 or 0 – ixodid Mar 17 '22 at 20:44
  • If I set `num_min <- 0` and `inclusive = TRUE` 0 is valid. – ismirsehregal Mar 17 '22 at 21:06
  • You are correct. Now enter -9 in Field A. The red ! appears. Great. But the button does not disable because shinyvalidate sees the field as 0 (I think). – ixodid Mar 17 '22 at 21:10
  • No `shinyvalidate` does what it should - `numericInputIcon` sets its value back to 0 (after providing a negative number) accordingly `is_valid` jumps back to `TRUE`. Using `numericInput` instead everything works as expected. – ismirsehregal Mar 17 '22 at 21:13
  • I'm sure you are correct. However, my question remains: Is it possible to get both the red "!" AND the disabled button to work if the valid numbers are: 0 <= x < 300 – ixodid Mar 17 '22 at 21:14
  • Not with the current behaviour of `numericInputIcon`. The procedure for "a" is: 100 = TRUE, -1 -> NA = FALSE, 0 = TRUE (just print it in `observe`). Not sure if it is intended to show "-1" and provide "0" as the input value in the end (the intermediate `NA` makes more sense). I'd file an issue [here](https://github.com/dreamRs/shinyWidgets) (or use `numericInput` instead). – ismirsehregal Mar 17 '22 at 21:20
  • Another workaround is setting `min = NULL, max = NULL` in `numericInputIcon` and let `shinyvalidate` do the work. – ismirsehregal Mar 17 '22 at 21:33
  • Setting min = NULL, max = NULL in numericInputIcon does not indicate which field is invalid with the red "!". It does disable the button. – ixodid Mar 18 '22 at 01:59
  • Yes, the "!" is the result of `shinyWidgets` built in validation. As mentioned before with `numericInputIcon` current behaviour you can't have both. You can display a message to the user *instead* (using `shinyvalidate`). For future reader [here](https://github.com/dreamRs/shinyWidgets/issues/483) you can find the related GitHub issue. – ismirsehregal Mar 18 '22 at 06:46

0 Answers0