ixodid
ixodid

Reputation: 2400

How reconcile Shinyvalidate sv_between and numericInput range

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)

Upvotes: 0

Views: 123

Answers (0)

Related Questions