shinyvalidate
shinyvalidate copied to clipboard
Allow for validators not related to an input
I have an app where the UI is revealed only as the user completes the prior UI section.
I'm using shinyvalidate to validate my inputs and it's GREAT. But some of my validation isn't entirely tied to inputs, and I'd love to just check a child validators for my state.
This comes up in 2 ways:
- I am using reactables as inputs, so they don't have a formal representation in
input
. - I don't want the validator to populate until an API call has returned, not when the input is selected.
App Architecture Sketch
I've tried to put together a simplified sketch/pseudocode of my use case below. I've left out a bunch of the more "on-label" use of shinyvalidate here, because that's clear. But unlike in the example below, my real app is more like 5 "normal" shinyvalidate cases and 2 unrelated to an input.
ui
div(
textInput("text1"),
tab_start_hidden(
id = "table_ui",
reactableOutput("table"),
textInput("text2")
),
hidden_ui(
id = "submit_ui",
actionButton("submit")
)
)
server
{
iv <- shinyvalidate::InputValidator$new()
iv$add_rule("text1", sv_required())
iv$add_rule("text2", sv_required())
iv$enable()
table_data <- eventReactive(input$text1, doApiCall(input$text1))
# Would love to just be able to check a validator here
observeEvent(table_data, if (table_data is not null) unhide("table_ui"))
output$table <- renderReactable(table_data)
selected <- get_reactable_row_selected("table")
# Would love to check child validator
observeEvent({selected, iv$is_valid()}, if (!is.null(selected()) & iv$is_valid()) unhide("submit_ui")
}
Could your issue be solved with something like validate(need(nrow(table_data) > 0, "Waiting on data"))
?
I think this would probably allow me to put a warning in the UI, but it still wouldn't allow me to simplify the "check if it's valid" logic the way just adding another rule to a shinyValidate
validator would, right?