I would like to disable the button if any field is out of range or contains non-numeric characters or is empty. If a field is in this condition I would like the red "!" within a circle to appear in the field.
The code below has the red circle appearing but the button is enabled.
If I change max = 1000
for inputId = a
then the invalidation of the button occurs but no red ! for a value of say 350.
How do I get the red ! and the disabled button at the same time? Another issue with the code below is that if you enter 500 in Field A and then 500 in Field B the red ! in Field A disappears. This doesn't occur if shinyvalidate is not used.
library("shiny")
library("bslib")
library("shinyjs")
library("shinyvalidate")
per_month <- "/mth"
num_min <- 0
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 = "160px",
icon = list(NULL, per_month)
),
),
div(class="col-4",
numericInputIcon(
inputId = "b",
label = "B",
value = 200,
min = num_min,
max = num_max,
width = "160px",
icon = list(NULL, per_month)
),
),
),
div(class = "row",
div(class = "col-12",
actionButton("cc_calculate", "—— Submit ——")
), align = "center"
)
)
)
server <- function(input, output, session) {
iv <- InputValidator$new()
iv$add_rule("a", sv_required(message = ""))
iv$add_rule("a", sv_between(num_min, num_max, message_fmt = ""))
iv$add_rule("b", sv_required(message = ""))
iv$add_rule("b", sv_between(num_min, num_max, message_fmt = ""))
iv$enable()
observe(
if (iv$is_valid()) {
enable("cc_calculate")
} else {
disable("cc_calculate")
}
)
}
shinyApp(ui = ui, server = server)