Skip to contents

Pushes a shadcn error message into the shad_form_field() wrapping a control, toggling data-invalid, aria-invalid, and aria-describedby and creating/removing the message element. This is the primitive that shad_validator() and shinyvalidate bridges build on.

Usage

update_shad_form_field(
  inputId,
  error = NULL,
  session = shiny::getDefaultReactiveDomain()
)

Arguments

inputId

The bound control's id (namespaced automatically).

error

Error text to show, or NULL to clear.

session

The Shiny session.

Value

Invisibly NULL.

Examples

if (interactive()) {
  ui <- page_shad(
    shad_form_field(shad_input("email"), label = "Email", inputId = "email"),
    shad_button("Check", inputId = "check")
  )
  server <- function(input, output, session) {
    shiny::observeEvent(input$check, {
      ok <- grepl("@", input$email, fixed = TRUE)
      update_shad_form_field("email", if (!ok) "Enter a valid email address.")
    })
  }
  shiny::shinyApp(ui, server)
}